Download Reactive-Resume/apps/server/src/auth/strategy/github.strategy.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/Reactive-Resume/apps/server/src/auth/strategy/github.strategy.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/Reactive-Resume/apps/server/src/auth/strategy/github.strategy.ts
-
curl -L -o github.strategy.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/Reactive-Resume/apps/server/src/auth/strategy/github.strategy.ts
2.09 kB
| import { BadRequestException, Injectable, Logger } from "@nestjs/common"; | |
| import { PassportStrategy } from "@nestjs/passport"; | |
| import { createId } from "@paralleldrive/cuid2"; | |
| import { User } from "@prisma/client"; | |
| import { ErrorMessage, processUsername } from "@reactive-resume/utils"; | |
| import { Profile, Strategy, StrategyOptions } from "passport-github2"; | |
| import { UserService } from "@/server/user/user.service"; | |
| () | |
| 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`).toLocaleLowerCase(); | |
| const picture = photos?.[0].value; | |
| let user: User | null = null; | |
| if (!email) throw new BadRequestException(ErrorMessage.InvalidCredentials); | |
| try { | |
| user = | |
| (await this.userService.findOneByIdentifier(email)) ?? | |
| (username ? await this.userService.findOneByIdentifier(username) : null); | |
| if (!user) throw new BadRequestException(ErrorMessage.InvalidCredentials); | |
| done(null, user); | |
| } catch { | |
| try { | |
| user = await this.userService.create({ | |
| email, | |
| picture, | |
| locale: "en-US", | |
| provider: "github", | |
| name: displayName || createId(), | |
| emailVerified: true, // auto-verify emails | |
| username: processUsername(username ?? email.split("@")[0]), | |
| secrets: { create: {} }, | |
| }); | |
| done(null, user); | |
| } catch (error) { | |
| Logger.error(error); | |
| throw new BadRequestException(ErrorMessage.UserAlreadyExists); | |
| } | |
| } | |
| } | |
| } | |