Skip to content

Commit 93b2141

Browse files
committed
fix: pull request feedbacks
1 parent 5313c35 commit 93b2141

File tree

5 files changed

+41
-22
lines changed

5 files changed

+41
-22
lines changed

src/features/auth/PageOAuthCallback.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React, { useEffect, useRef } from 'react';
22

3-
import { useParams, useRouter, useSearchParams } from 'next/navigation';
3+
import {
4+
notFound,
5+
useParams,
6+
useRouter,
7+
useSearchParams,
8+
} from 'next/navigation';
49
import { useTranslation } from 'react-i18next';
510
import { z } from 'zod';
611

@@ -17,7 +22,9 @@ export default function PageOAuthCallback() {
1722
const toastError = useToastError();
1823
const router = useRouter();
1924
const isTriggeredRef = useRef(false);
20-
const params = z.object({ provider: zOAuthProvider() }).parse(useParams());
25+
const params = z
26+
.object({ provider: zOAuthProvider() })
27+
.safeParse(useParams());
2128
const searchParams = z
2229
.object({ code: z.string(), state: z.string() })
2330
.safeParse({
@@ -43,10 +50,14 @@ export default function PageOAuthCallback() {
4350
if (isTriggeredRef.current) return;
4451
isTriggeredRef.current = true;
4552

53+
if (!(params.success && searchParams.success)) {
54+
notFound();
55+
}
56+
4657
validateLogin.mutate({
47-
provider: params.provider,
48-
code: searchParams.data?.code ?? '',
49-
state: searchParams.data?.state ?? '',
58+
provider: params.data.provider,
59+
code: searchParams.data.code,
60+
state: searchParams.data.state,
5061
language: i18n.language,
5162
});
5263
};

src/server/config/oauth/providers/discord.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { OAuthClient, getOAuthCallbackUrl } from '@/server/config/oauth/utils';
88
const zDiscordUser = () =>
99
z.object({
1010
id: z.string(),
11+
username: z.string().nullish(),
1112
global_name: z.string().nullish(),
1213
email: z.string().email().nullish(),
1314
verified: z.boolean().nullish(),
@@ -29,7 +30,7 @@ export const discord: OAuthClient = {
2930
if (!discordClient) {
3031
throw new TRPCError({
3132
code: 'NOT_IMPLEMENTED',
32-
message: 'Missing Discord environnement variables',
33+
message: 'Missing Discord environment variables',
3334
});
3435
}
3536
return await discordClient.createAuthorizationURL(state, {
@@ -40,7 +41,7 @@ export const discord: OAuthClient = {
4041
if (!discordClient) {
4142
throw new TRPCError({
4243
code: 'NOT_IMPLEMENTED',
43-
message: 'Missing Discord environnement variables',
44+
message: 'Missing Discord environment variables',
4445
});
4546
}
4647
return discordClient.validateAuthorizationCode(code);
@@ -61,7 +62,7 @@ export const discord: OAuthClient = {
6162
}
6263

6364
const userData = await userResponse.json();
64-
ctx.logger.debug(userData);
65+
ctx.logger.info('User data retrieved from Discord');
6566

6667
ctx.logger.info('Parse the Discord user');
6768
const discordUser = zDiscordUser().safeParse(userData);
@@ -76,7 +77,7 @@ export const discord: OAuthClient = {
7677

7778
return {
7879
id: discordUser.data.id,
79-
name: discordUser.data.global_name,
80+
name: discordUser.data.global_name ?? discordUser.data.username,
8081
email: discordUser.data.email,
8182
isEmailVerified: !!discordUser.data.verified,
8283
language: discordUser.data.locale,

src/server/config/oauth/providers/github.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const github: OAuthClient = {
3434
if (!githubClient) {
3535
throw new TRPCError({
3636
code: 'NOT_IMPLEMENTED',
37-
message: 'Missing GitHub environnement variables',
37+
message: 'Missing GitHub environment variables',
3838
});
3939
}
4040
return await githubClient.createAuthorizationURL(state, {
@@ -45,7 +45,7 @@ export const github: OAuthClient = {
4545
if (!githubClient) {
4646
throw new TRPCError({
4747
code: 'NOT_IMPLEMENTED',
48-
message: 'Missing GitHub environnement variables',
48+
message: 'Missing GitHub environment variables',
4949
});
5050
}
5151
return githubClient.validateAuthorizationCode(code);
@@ -80,7 +80,7 @@ export const github: OAuthClient = {
8080
}
8181

8282
const emailsData = await emailsResponse.json();
83-
ctx.logger.debug(emailsData);
83+
ctx.logger.info('Retrieved emails from GitHub');
8484

8585
ctx.logger.info('Parse the GitHub user emails');
8686
const emails = zGitHubEmails().safeParse(emailsData);
@@ -98,7 +98,7 @@ export const github: OAuthClient = {
9898
const primaryEmail = emails.data?.find((email) => email.primary) ?? null;
9999

100100
const userData = await userResponse.json();
101-
ctx.logger.debug(userData);
101+
ctx.logger.info('User data retrieved from GitHub');
102102

103103
ctx.logger.info('Parse the GitHub user');
104104
const gitHubUser = zGitHubUser().safeParse(userData);

src/server/config/oauth/providers/google.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ export const google: OAuthClient = {
2828
if (!googleClient) {
2929
throw new TRPCError({
3030
code: 'NOT_IMPLEMENTED',
31-
message: 'Missing Google environnement variables',
31+
message: 'Missing Google environment variables',
32+
});
33+
}
34+
if (!codeVerifier) {
35+
throw new TRPCError({
36+
code: 'BAD_REQUEST',
37+
message: 'Missing codeVerifier',
3238
});
3339
}
34-
if (!codeVerifier) throw new Error('Missing codeVerifier');
3540
return await googleClient.createAuthorizationURL(state, codeVerifier, {
3641
scopes: ['email', 'profile'],
3742
});
@@ -40,7 +45,7 @@ export const google: OAuthClient = {
4045
if (!googleClient) {
4146
throw new TRPCError({
4247
code: 'NOT_IMPLEMENTED',
43-
message: 'Missing Google environnement variables',
48+
message: 'Missing Google environment variables',
4449
});
4550
}
4651
if (!codeVerifier) throw new Error('Missing codeVerifier');
@@ -65,7 +70,7 @@ export const google: OAuthClient = {
6570
}
6671

6772
const userData = await userResponse.json();
68-
ctx.logger.debug(userData);
73+
ctx.logger.info('User data retrieved from Google');
6974

7075
ctx.logger.info('Parse the Google user');
7176
const googleUser = zGoogleUser().safeParse(userData);

src/server/routers/oauth.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ export const oauthRouter = createTRPCRouter({
9696
oAuthProvider(input.provider).shouldUseCodeVerifier &&
9797
!codeVerifierFromCookie.data
9898
) {
99-
ctx.logger.warn('Missing oAuth codeVerifier');
99+
ctx.logger.warn('Invalid or expired authorization request');
100100
throw new TRPCError({
101101
code: 'BAD_REQUEST',
102-
message: 'Missing oAuth codeVerifier',
102+
message: 'Invalid or expired authorization request',
103103
});
104104
}
105105

@@ -176,16 +176,18 @@ export const oauthRouter = createTRPCRouter({
176176
}
177177

178178
if (existingUser?.accountStatus === 'DISABLED') {
179+
ctx.logger.info('Account is disabled');
179180
throw new TRPCError({
180181
code: 'UNAUTHORIZED',
181-
message: 'Account is disabled',
182+
message: 'Please verify your account to proceed',
182183
});
183184
}
184185

185186
if (existingUser?.accountStatus === 'NOT_VERIFIED') {
187+
ctx.logger.error('Account should not be NOT_VERIFIED at this point');
186188
throw new TRPCError({
187-
code: 'INTERNAL_SERVER_ERROR',
188-
message: 'Account should not be NOT_VERIFIED at this point',
189+
code: 'UNAUTHORIZED',
190+
message: 'Please verify your account to proceed',
189191
});
190192
}
191193

0 commit comments

Comments
 (0)