Skip to content

Commit 56a3874

Browse files
author
DylanBulmer
committed
remove nesting; add error msg to jwt gen func
1 parent 38a8b7d commit 56a3874

File tree

3 files changed

+75
-80
lines changed

3 files changed

+75
-80
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codrjs/core",
3-
"version": "1.0.6-patch4",
3+
"version": "1.0.6-patch5",
44
"description": "An open-sourced customizable annotation tool",
55
"main": "./cjs/index.js",
66
"module": "./esm/index.js",

src/classes/JWT.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ export function generateToken(payload: IUser) {
6464
subject: payload?._id,
6565
};
6666
return jwt.sign(payload, <string>process.env.JWT_SECRET, signOpts);
67-
} catch (err) {
68-
throw new Error({ status: 500, message: <string>err });
67+
} catch (err: any) {
68+
throw new Error({
69+
status: 500,
70+
message: err?.message || "Could generate the JWT token",
71+
});
6972
}
7073
}

src/services/auth.ts

Lines changed: 69 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -45,98 +45,90 @@ class Authentication {
4545
});
4646
}
4747

48-
try {
49-
const user = await User.findOne({ email });
50-
if (!user) {
51-
// is user cannot be found, then they are not allowed in.
52-
throw new Error({
53-
status: 401,
54-
message:
55-
"It appears you do not have an account using this email, please contact your Codr admin to gain access.",
48+
const user = await User.findOne({ email });
49+
if (!user) {
50+
// is user cannot be found, then they are not allowed in.
51+
throw new Error({
52+
status: 401,
53+
message:
54+
"It appears you do not have an account using this email, please contact your Codr admin to gain access.",
55+
});
56+
} else if (!token) {
57+
try {
58+
// init access token
59+
const uuid = uuidv4();
60+
const accessToken = new AccessToken(uuid);
61+
await user.updateOne({
62+
accessToken: accessToken.encode(),
5663
});
57-
} else if (!token) {
58-
try {
59-
// init access token
60-
const uuid = uuidv4();
61-
const accessToken = new AccessToken(uuid);
62-
await user.updateOne({
63-
accessToken: accessToken.encode(),
64-
});
65-
66-
// send email with access code/token
67-
const link =
68-
`${process.env.HOST}${process.env.API_PATH}` +
69-
"/auth/email/verify?token=" +
70-
encrypt(JSON.stringify({ email: email, token: uuid }));
71-
const template = new SigninTemplate();
72-
await Mail.send(await template.html({ link }), {
73-
...template.config,
74-
to: email,
75-
});
76-
return new Response({
77-
message: "An email has been sent to your inbox.",
78-
});
79-
} catch (e: any) {
80-
throw new Error({
81-
status: 500,
82-
message: e?.message || "An unknown error occured",
83-
});
84-
}
85-
} else if (user.accessToken) {
86-
// decrypt the stored access code
87-
const accessToken = new AccessToken(user.accessToken);
8864

89-
// check if:
90-
// * the tokens match
91-
// * the token was created less than 5 minutes ago
92-
// * and the token is not expired (has not been used already)
93-
if (accessToken.isValid(token)) {
65+
// send email with access code/token
66+
const link =
67+
`${process.env.HOST}${process.env.API_PATH}` +
68+
"/auth/email/verify?token=" +
69+
encrypt(JSON.stringify({ email: email, token: uuid }));
70+
const template = new SigninTemplate();
71+
await Mail.send(await template.html({ link }), {
72+
...template.config,
73+
to: email,
74+
});
75+
return new Response({
76+
message: "An email has been sent to your inbox.",
77+
});
78+
} catch (e: any) {
79+
throw new Error({
80+
status: 500,
81+
message: e?.message || "An unknown error occured",
82+
details: e,
83+
});
84+
}
85+
} else if (user.accessToken) {
86+
// decrypt the stored access code
87+
const accessToken = new AccessToken(user.accessToken);
9488

95-
// update access token
96-
accessToken.use();
89+
// check if:
90+
// * the tokens match
91+
// * the token was created less than 5 minutes ago
92+
// * and the token is not expired (has not been used already)
93+
if (accessToken.isValid(token)) {
94+
// update access token
95+
accessToken.use();
9796

98-
// init user update
99-
const update = {
100-
accessToken: accessToken.encode(),
101-
refreshToken: new AccessToken(uuidv4()).encode(),
102-
};
97+
// init user update
98+
const update = {
99+
accessToken: accessToken.encode(),
100+
refreshToken: new AccessToken(uuidv4()).encode(),
101+
};
103102

104-
try {
105-
// update user
106-
await user.updateOne(update);
103+
try {
104+
// update user
105+
await user.updateOne(update);
107106

108-
// generate JWT token
109-
const token = generateToken({ ...user, ...update } as IUser);
107+
// generate JWT token
108+
const token = generateToken({ ...user, ...update } as IUser);
110109

111-
// send response
112-
return new Response<{ token: string }>({
113-
message: `Login successful.`,
114-
details: { token },
115-
});
116-
} catch (e: any) {
117-
throw new Error({
118-
status: 500,
119-
message:
120-
e?.message ||
121-
"An unexpected error occured while updating a user.",
122-
});
123-
}
124-
} else
110+
// send response
111+
return new Response<{ token: string }>({
112+
message: `Login successful.`,
113+
details: { token },
114+
});
115+
} catch (e: any) {
125116
throw new Error({
126117
status: 500,
127-
message: "Login link expired or is invalid.",
118+
message: "An unexpected error occured while updating a user.",
119+
details: e,
128120
});
129-
} else {
121+
}
122+
} else
130123
throw new Error({
131124
status: 500,
132-
message:
133-
"An unknown error occured while authenticating an access token.",
125+
message: "Login link expired or is invalid.",
134126
});
135-
}
136-
} catch (e: any) {
127+
} else {
137128
throw new Error({
138129
status: 500,
139-
message: e?.message || "An unknown error occured",
130+
message:
131+
"An unknown error occured while authenticating an access token.",
140132
});
141133
}
142134
}

0 commit comments

Comments
 (0)