Skip to content

Commit

Permalink
🔒 Improve JWT system
Browse files Browse the repository at this point in the history
  • Loading branch information
vbetsch committed Apr 8, 2024
1 parent 1f9c3fe commit 5455f47
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions .env.local.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
MONGODB_URI=
MONGO_DATABASE=
API_TOKEN=
SECRET_JWT=
REACT_EDITOR=
2 changes: 1 addition & 1 deletion pages/api/auth/signin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}

try {
token = await signJwt('SECRET_JWT', userFound);
token = await signJwt(userFound);
} catch (e) {
const errorMessage: string = 'Unable to create jwt';
console.error(`ERROR: ${errorMessage} -> ${e instanceof Error ? e.message : e}`);
Expand Down
20 changes: 11 additions & 9 deletions src/services/jsonwebtoken.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import jwt from 'jsonwebtoken';

const signJwt = async (
key: string,
value: string | object,
options: jwt.SignOptions | undefined = undefined,
): Promise<string> => {
return jwt.sign(value, key, options);
import jwt, { JwtPayload } from 'jsonwebtoken';

const SECRET_JWT: string = process.env.SECRET_JWT || 'SECRET_JWT';

const signJwt = async (value: string | object, options: jwt.SignOptions | undefined = undefined): Promise<string> => {
return jwt.sign(value, SECRET_JWT, options);
};

const verifyJwt = async (token: string): Promise<JwtPayload | string> => {
return jwt.verify(token, SECRET_JWT);
};

export { signJwt };
export { signJwt, verifyJwt };

0 comments on commit 5455f47

Please sign in to comment.