Skip to content

Commit 7a48e15

Browse files
committed
Return redirect url for oauth redirect
1 parent c4ee3c3 commit 7a48e15

File tree

5 files changed

+114
-117
lines changed

5 files changed

+114
-117
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ dist
105105
.idea
106106

107107
# docker
108-
.docker
108+
packages/server/.docker

packages/server/src/modules/auth/controllers/combinedAuthController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ export default function initializeCombinedAuthController({
3939
const router = Router();
4040

4141
router.use(initializeEmailAuthController({
42-
jwtInfo,
4342
prisma,
4443
emailService,
4544
emailTemplatesService,
45+
callbackUrl,
4646
}));
4747

4848
router.use(initializeSessionController({

packages/server/src/modules/auth/controllers/emailAuthController.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@ import { TypedRequest } from "../../shared/types/express";
1010
import { getIp } from "../../shared/utils/getIp";
1111
import { EmailService } from "../../emails/services/emailService";
1212
import { EmailTemplates } from "../../emails/services/emailTemplates";
13-
import { JwtInfo } from "../utils/jwtInfo";
1413
import { getHashedPassword } from "../utils/passwordManager";
15-
import { createSession } from "../utils/sessionManager";
14+
import { redirectWithCode } from "../../shared/utils/redirectManager";
1615

1716
export default function initializeEmailAuthController({
18-
jwtInfo,
1917
prisma,
2018
emailService,
2119
emailTemplatesService,
20+
callbackUrl,
2221
}: {
23-
jwtInfo: JwtInfo;
2422
prisma: PrismaClient;
2523
emailService: EmailService;
2624
emailTemplatesService: EmailTemplates;
25+
callbackUrl: string;
26+
2727
}): Router {
2828
const router = Router();
2929

@@ -123,20 +123,19 @@ export default function initializeEmailAuthController({
123123
}
124124
}
125125

126-
const ip = getIp(req);
127-
128-
const { jwtToken, jwtRefreshToken } = await createSession({
129-
prisma,
130-
userId: user.id,
131-
ip,
132-
jwtInfo,
133-
userAgent: req.headers["user-agent"] || "",
134-
authProviderType: "email",
126+
const authorizationCode = await prisma.authorizationCode.create({
127+
data: {
128+
userId: user.id,
129+
authProviderType: "email",
130+
expiresAt: dayjs().add(5, "minutes").toDate(),
131+
},
135132
});
136133

137134
return res.json({
138-
accessToken: jwtToken,
139-
refreshToken: jwtRefreshToken,
135+
redirectUrl: redirectWithCode({
136+
callbackUrl,
137+
code: authorizationCode.id,
138+
}),
140139
});
141140
} catch (error) {
142141
return next(error);

packages/server/src/modules/auth/controllers/sessionController.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,103 @@ export default function initializeSessionController({
191191
},
192192
);
193193

194+
const exchangeCodeSchema = {
195+
body: {
196+
code: Joi.string().required(),
197+
},
198+
};
199+
200+
router.post(
201+
"/exchange",
202+
validation(exchangeCodeSchema),
203+
async (req: TypedRequest<typeof exchangeCodeSchema>, res, next) => {
204+
try {
205+
const { code } = req.body;
206+
207+
const authorizationCode = await prisma.authorizationCode.findFirst({
208+
select: {
209+
id: true,
210+
userId: true,
211+
authProviderType: true,
212+
expiresAt: true,
213+
},
214+
where: {
215+
id: code,
216+
},
217+
});
218+
219+
if (!authorizationCode) {
220+
return errorResponse({
221+
response: res,
222+
message: "Authorization code not found",
223+
status: 404,
224+
error: "AuthorizationCodeNotFound",
225+
});
226+
}
227+
228+
if (dayjs(authorizationCode.expiresAt).isBefore(dayjs())) {
229+
return errorResponse({
230+
response: res,
231+
message: "Authorization code expired",
232+
status: 400,
233+
error: "AuthorizationCodeExpired",
234+
});
235+
}
236+
237+
const user = await prisma.user.findFirst({
238+
select: {
239+
id: true,
240+
authProviderType: true,
241+
},
242+
where: {
243+
id: authorizationCode.userId,
244+
},
245+
});
246+
247+
if (!user) {
248+
return errorResponse({
249+
response: res,
250+
message: "User not found",
251+
status: 404,
252+
error: "UserNotFound",
253+
});
254+
}
255+
256+
if (user.authProviderType !== authorizationCode.authProviderType) {
257+
return errorResponse({
258+
response: res,
259+
message: "Invalid provider",
260+
status: 400,
261+
error: "InvalidProvider",
262+
});
263+
}
264+
265+
const ip = getIp(req);
266+
267+
await prisma.authorizationCode.delete({
268+
where: {
269+
id: authorizationCode.id,
270+
},
271+
});
272+
273+
const { jwtToken, jwtRefreshToken } = await createSession({
274+
prisma,
275+
userId: user.id,
276+
ip,
277+
jwtInfo,
278+
userAgent: req.headers["user-agent"] || "",
279+
authProviderType: user.authProviderType,
280+
});
281+
282+
return res.json({
283+
accessToken: jwtToken,
284+
refreshToken: jwtRefreshToken,
285+
});
286+
} catch (error) {
287+
return next(error);
288+
}
289+
},
290+
);
291+
194292
return router;
195293
}

packages/server/src/modules/auth/controllers/socialAuthController.ts

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { Router } from "express";
33
import Joi from "joi";
44
import dayjs from "dayjs";
55
import url from "url";
6-
import errorResponse from "../../shared/utils/errorResponse";
76
import validation from "../../shared/middlewares/validation";
87
import { TypedRequest } from "../../shared/types/express";
98
import { getIp } from "../../shared/utils/getIp";
109
import { JwtInfo } from "../utils/jwtInfo";
11-
import { createSession } from "../utils/sessionManager";
1210
import { redirectWithCode, redirectWithError } from "../../shared/utils/redirectManager";
1311

1412
async function authenticate({
@@ -295,103 +293,5 @@ export default function initializeSocialAuthController({
295293
},
296294
);
297295

298-
const exchangeCodeSchema = {
299-
body: {
300-
code: Joi.string().required(),
301-
},
302-
};
303-
304-
router.post(
305-
"/exchange",
306-
validation(exchangeCodeSchema),
307-
async (req: TypedRequest<typeof exchangeCodeSchema>, res, next) => {
308-
try {
309-
const { code } = req.body;
310-
311-
const authorizationCode = await prisma.authorizationCode.findFirst({
312-
select: {
313-
id: true,
314-
userId: true,
315-
authProviderType: true,
316-
expiresAt: true,
317-
},
318-
where: {
319-
id: code,
320-
},
321-
});
322-
323-
if (!authorizationCode) {
324-
return errorResponse({
325-
response: res,
326-
message: "Authorization code not found",
327-
status: 404,
328-
error: "AuthorizationCodeNotFound",
329-
});
330-
}
331-
332-
if (dayjs(authorizationCode.expiresAt).isBefore(dayjs())) {
333-
return errorResponse({
334-
response: res,
335-
message: "Authorization code expired",
336-
status: 400,
337-
error: "AuthorizationCodeExpired",
338-
});
339-
}
340-
341-
const user = await prisma.user.findFirst({
342-
select: {
343-
id: true,
344-
authProviderType: true,
345-
},
346-
where: {
347-
id: authorizationCode.userId,
348-
},
349-
});
350-
351-
if (!user) {
352-
return errorResponse({
353-
response: res,
354-
message: "User not found",
355-
status: 404,
356-
error: "UserNotFound",
357-
});
358-
}
359-
360-
if (user.authProviderType !== authorizationCode.authProviderType) {
361-
return errorResponse({
362-
response: res,
363-
message: "Invalid provider",
364-
status: 400,
365-
error: "InvalidProvider",
366-
});
367-
}
368-
369-
const ip = getIp(req);
370-
371-
await prisma.authorizationCode.delete({
372-
where: {
373-
id: authorizationCode.id,
374-
},
375-
});
376-
377-
const { jwtToken, jwtRefreshToken } = await createSession({
378-
prisma,
379-
userId: user.id,
380-
ip,
381-
jwtInfo,
382-
userAgent: req.headers["user-agent"] || "",
383-
authProviderType: user.authProviderType,
384-
});
385-
386-
return res.json({
387-
accessToken: jwtToken,
388-
refreshToken: jwtRefreshToken,
389-
});
390-
} catch (error) {
391-
return next(error);
392-
}
393-
},
394-
);
395-
396296
return router;
397297
}

0 commit comments

Comments
 (0)