mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2026-07-26 09:54:43 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { BadRequestException, Injectable, UnauthorizedException } from "@nestjs/common";
|
||||
import { PassportStrategy } from "@nestjs/passport";
|
||||
import { User } from "@prisma/client";
|
||||
import { processUsername } from "@reactive-resume/utils";
|
||||
import { Profile, Strategy, StrategyOptions } from "passport-github2";
|
||||
|
||||
import { ErrorMessage } from "@/server/constants/error-message";
|
||||
import { UserService } from "@/server/user/user.service";
|
||||
|
||||
@Injectable()
|
||||
export class GitHubStrategy extends PassportStrategy(Strategy, "github") {
|
||||
constructor(
|
||||
readonly clientID: string,
|
||||
readonly clientSecret: string,
|
||||
readonly callbackURL: string,
|
||||
private readonly userService: UserService,
|
||||
) {
|
||||
super({ clientID, clientSecret, callbackURL, scope: ["user:email"] } as StrategyOptions);
|
||||
}
|
||||
|
||||
async validate(
|
||||
_accessToken: string,
|
||||
_refreshToken: string,
|
||||
profile: Profile,
|
||||
done: (err?: string | Error | null, user?: Express.User, info?: unknown) => void,
|
||||
) {
|
||||
const { displayName, emails, photos, username } = profile;
|
||||
|
||||
const email = emails?.[0].value ?? `${username}@github.com`;
|
||||
const picture = photos?.[0].value;
|
||||
|
||||
let user: User | null = null;
|
||||
|
||||
if (!email) throw new BadRequestException();
|
||||
|
||||
try {
|
||||
const user = await this.userService.findOneByIdentifier(email);
|
||||
|
||||
if (!user) throw new UnauthorizedException();
|
||||
|
||||
done(null, user);
|
||||
} catch (error) {
|
||||
try {
|
||||
user = await this.userService.create({
|
||||
email,
|
||||
picture,
|
||||
language: "en",
|
||||
name: displayName,
|
||||
provider: "github",
|
||||
emailVerified: true, // auto-verify emails
|
||||
username: processUsername(username ?? email.split("@")[0]),
|
||||
secrets: { create: {} },
|
||||
});
|
||||
|
||||
done(null, user);
|
||||
} catch (error) {
|
||||
throw new BadRequestException(ErrorMessage.UserAlreadyExists);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user