Skip to content

feat: 検証可能なシリアル番号を生成 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/api/coupons/route.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import sha256 from 'crypto-js/sha256';
import {kv} from "@vercel/kv";
import {NextRequest, NextResponse} from "next/server";
import luhn from "luhn-js";

function hashSerialNumber(serialNumber: string, passCode: string) : string {
return serialNumber + sha256(`${passCode}:${process.env.SALT}`);
}

function generateId() {
let key = '';
const length = 12;
const length = 11;
const characters = '0123456789';
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
key += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
key = luhn.generate(key);
return key;
}

Expand Down
16 changes: 15 additions & 1 deletion app/update/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client'
import React from 'react';
import { ChangeEvent, FormEvent, useState } from "react";
import luhn from "luhn-js";
import 'bootstrap/dist/css/bootstrap.min.css'

export default function Update() {
Expand Down Expand Up @@ -31,8 +32,21 @@ export default function Update() {

event.preventDefault()

const serialNumberStr= serialNumber.replace(/[^0-9]/g, '')

if(!luhn.isValid(serialNumberStr)) {
setUpdateMessage(<div className="card text-bg-danger">
<div className="card-body">
正しいシリアル番号を入力してください
</div>
</div>)
setSubmitUpdateDisabled(false)
return
}

const formData = new FormData(event.currentTarget)
formData.set("serialNumber", serialNumber.replace(/[^0-9]/g, ''))
formData.set("serialNumber", serialNumberStr)

const response = await fetch('/api/coupons', {
method: 'PUT',
body: JSON.stringify(Object.fromEntries(formData)),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"crypto-js": "^4.2.0",
"eslint": "8.38.0",
"eslint-config-next": "13.3.0",
"luhn-js": "^1.1.2",
"next": "13.4.13",
"postcss": "8.4.22",
"react": "18.2.0",
Expand Down