Skip to content

Commit

Permalink
feat(oauth): handle authorizing without role
Browse files Browse the repository at this point in the history
  • Loading branch information
Veirt committed Nov 30, 2024
1 parent e4e47ca commit d45548c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/common/oauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export class OAuthService {
}
}

async authenticateWithGoogle(idToken: string, role: "learner" | "tutor") {
async authenticateWithGoogle(idToken: string, role?: "learner" | "tutor") {
const userData = await this.verifyGoogleToken(idToken);

// Check if user exists
let user = await this.authRepository.findUserByEmail(userData.email);

if (!user) {
if (!user && role) {
// Register new user
const result = await this.authRepository.registerUser({
email: userData.email,
Expand All @@ -57,16 +57,18 @@ export class OAuthService {
};
}

if (!user) throw new Error("Role is required for new users");

const token = generateJWT({
id: user!.id,
role: user!.role,
id: user.id,
role: user.role,
});

return {
token,
user: {
id: user!.id,
role: user!.role,
id: user.id,
role: user.role,
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/module/auth/auth.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type FCMTokenSchema = z.infer<typeof fcmTokenSchema>;
export const oAuthSchema = z.object({
body: z.object({
idToken: z.string(),
role: z.enum(["learner", "tutor"]),
role: z.enum(["learner", "tutor"]).optional(),
}),
});

Expand Down

0 comments on commit d45548c

Please sign in to comment.