Download apps/backend/src/api/routes/auth.controller.ts from Leon4gr45/postio: direct link, hf CLI and curl.
- Browser
- Download file 8.39 kB
-
https://huggingface.co/spaces/Leon4gr45/postio/resolve/main/apps/backend/src/api/routes/auth.controller.ts
- Command line
-
hf download hf://spaces/Leon4gr45/postio/apps/backend/src/api/routes/auth.controller.ts
-
curl -L -o auth.controller.ts https://huggingface.co/spaces/Leon4gr45/postio/resolve/main/apps/backend/src/api/routes/auth.controller.ts
8.39 kB
| import { | |
| Body, | |
| Controller, | |
| Get, | |
| Param, | |
| Post, | |
| Query, | |
| Req, | |
| Res, | |
| } from '@nestjs/common'; | |
| import { Response, Request } from 'express'; | |
| import { CreateOrgUserDto } from '@gitroom/nestjs-libraries/dtos/auth/create.org.user.dto'; | |
| import { LoginUserDto } from '@gitroom/nestjs-libraries/dtos/auth/login.user.dto'; | |
| import { AuthService } from '@gitroom/backend/services/auth/auth.service'; | |
| import { ForgotReturnPasswordDto } from '@gitroom/nestjs-libraries/dtos/auth/forgot-return.password.dto'; | |
| import { ForgotPasswordDto } from '@gitroom/nestjs-libraries/dtos/auth/forgot.password.dto'; | |
| import { ResendActivationDto } from '@gitroom/nestjs-libraries/dtos/auth/resend-activation.dto'; | |
| import { ApiTags } from '@nestjs/swagger'; | |
| import { getCookieUrlFromDomain } from '@gitroom/helpers/subdomain/subdomain.management'; | |
| import { EmailService } from '@gitroom/nestjs-libraries/services/email.service'; | |
| import { RealIP } from 'nestjs-real-ip'; | |
| import { UserAgent } from '@gitroom/nestjs-libraries/user/user.agent'; | |
| import { Provider } from '@prisma/client'; | |
| import * as Sentry from '@sentry/nestjs'; | |
| ('Auth') | |
| ('/auth') | |
| export class AuthController { | |
| constructor( | |
| private _authService: AuthService, | |
| private _emailService: EmailService | |
| ) {} | |
| ('/can-register') | |
| async canRegister() { | |
| return { | |
| register: await this._authService.canRegister(Provider.LOCAL as string), | |
| }; | |
| } | |
| ('/register') | |
| async register( | |
| () req: Request, | |
| () body: CreateOrgUserDto, | |
| ({ passthrough: false }) response: Response, | |
| () ip: string, | |
| () userAgent: string | |
| ) { | |
| try { | |
| const getOrgFromCookie = this._authService.getOrgFromCookie( | |
| req?.cookies?.org | |
| ); | |
| const { jwt, addedOrg } = await this._authService.routeAuth( | |
| body.provider, | |
| body, | |
| ip, | |
| userAgent, | |
| getOrgFromCookie | |
| ); | |
| const activationRequired = | |
| body.provider === 'LOCAL' && this._emailService.hasProvider(); | |
| if (activationRequired) { | |
| response.header('activate', 'true'); | |
| response.status(200).json({ activate: true }); | |
| return; | |
| } | |
| response.cookie('auth', jwt, { | |
| domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!), | |
| ...(!process.env.NOT_SECURED | |
| ? { | |
| secure: true, | |
| httpOnly: true, | |
| sameSite: 'none', | |
| } | |
| : {}), | |
| expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), | |
| }); | |
| if (process.env.NOT_SECURED) { | |
| response.header('auth', jwt); | |
| } | |
| if (typeof addedOrg !== 'boolean' && addedOrg?.organizationId) { | |
| response.cookie('showorg', addedOrg.organizationId, { | |
| domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!), | |
| ...(!process.env.NOT_SECURED | |
| ? { | |
| secure: true, | |
| httpOnly: true, | |
| sameSite: 'none', | |
| } | |
| : {}), | |
| expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), | |
| }); | |
| if (process.env.NOT_SECURED) { | |
| response.header('showorg', addedOrg.organizationId); | |
| } | |
| } | |
| Sentry.metrics.count('new_user', 1); | |
| response.header('onboarding', 'true'); | |
| response.status(200).json({ | |
| register: true, | |
| }); | |
| } catch (e: any) { | |
| response.status(400).send(e.message); | |
| } | |
| } | |
| ('/login') | |
| async login( | |
| () req: Request, | |
| () body: LoginUserDto, | |
| ({ passthrough: false }) response: Response, | |
| () ip: string, | |
| () userAgent: string | |
| ) { | |
| try { | |
| const getOrgFromCookie = this._authService.getOrgFromCookie( | |
| req?.cookies?.org | |
| ); | |
| const { jwt, addedOrg } = await this._authService.routeAuth( | |
| body.provider, | |
| body, | |
| ip, | |
| userAgent, | |
| getOrgFromCookie | |
| ); | |
| response.cookie('auth', jwt, { | |
| domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!), | |
| ...(!process.env.NOT_SECURED | |
| ? { | |
| secure: true, | |
| httpOnly: true, | |
| sameSite: 'none', | |
| } | |
| : {}), | |
| expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), | |
| }); | |
| if (process.env.NOT_SECURED) { | |
| response.header('auth', jwt); | |
| } | |
| if (typeof addedOrg !== 'boolean' && addedOrg?.organizationId) { | |
| response.cookie('showorg', addedOrg.organizationId, { | |
| domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!), | |
| ...(!process.env.NOT_SECURED | |
| ? { | |
| secure: true, | |
| httpOnly: true, | |
| sameSite: 'none', | |
| } | |
| : {}), | |
| expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), | |
| }); | |
| if (process.env.NOT_SECURED) { | |
| response.header('showorg', addedOrg.organizationId); | |
| } | |
| } | |
| response.header('reload', 'true'); | |
| response.status(200).json({ | |
| login: true, | |
| }); | |
| } catch (e: any) { | |
| response.status(400).send(e.message); | |
| } | |
| } | |
| ('/forgot') | |
| async forgot(() body: ForgotPasswordDto) { | |
| try { | |
| await this._authService.forgot(body.email); | |
| return { | |
| forgot: true, | |
| }; | |
| } catch (e) { | |
| return { | |
| forgot: false, | |
| }; | |
| } | |
| } | |
| ('/forgot-return') | |
| async forgotReturn(() body: ForgotReturnPasswordDto) { | |
| const reset = await this._authService.forgotReturn(body); | |
| return { | |
| reset: !!reset, | |
| }; | |
| } | |
| ('/oauth-mobile-callback') | |
| mobileCallback( | |
| ('code') code: string, | |
| ('state') state: string, | |
| ({ passthrough: false }) response: Response | |
| ) { | |
| const scheme = process.env.MOBILE_APP_SCHEME || 'postiz://auth/callback'; | |
| const params = new URLSearchParams(); | |
| if (code) params.set('code', code); | |
| if (state) params.set('state', state); | |
| return response.redirect(302, `${scheme}?${params.toString()}`); | |
| } | |
| ('/oauth/:provider') | |
| async oauthLink(('provider') provider: string, () query: any) { | |
| return this._authService.oauthLink(provider, query); | |
| } | |
| ('/activate') | |
| async activate( | |
| ('code') code: string, | |
| ('datafast_visitor_id') datafast_visitor_id: string, | |
| ({ passthrough: false }) response: Response | |
| ) { | |
| const activate = await this._authService.activate( | |
| code, | |
| datafast_visitor_id | |
| ); | |
| if (!activate) { | |
| return response.status(200).json({ can: false }); | |
| } | |
| response.cookie('auth', activate, { | |
| domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!), | |
| ...(!process.env.NOT_SECURED | |
| ? { | |
| secure: true, | |
| httpOnly: true, | |
| sameSite: 'none', | |
| } | |
| : {}), | |
| expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), | |
| }); | |
| if (process.env.NOT_SECURED) { | |
| response.header('auth', activate); | |
| } | |
| response.header('onboarding', 'true'); | |
| return response.status(200).json({ can: true }); | |
| } | |
| ('/resend-activation') | |
| async resendActivation(() body: ResendActivationDto) { | |
| try { | |
| await this._authService.resendActivationEmail(body.email); | |
| return { | |
| success: true, | |
| }; | |
| } catch (e: any) { | |
| return { | |
| success: false, | |
| message: e.message, | |
| }; | |
| } | |
| } | |
| ('/oauth/:provider/exists') | |
| async oauthExists( | |
| ('code') code: string, | |
| ('redirect_uri') redirect_uri: string, | |
| ('provider') provider: string, | |
| ({ passthrough: false }) response: Response | |
| ) { | |
| const { jwt, token } = await this._authService.checkExists( | |
| provider, | |
| code, | |
| redirect_uri | |
| ); | |
| if (token) { | |
| return response.json({ token }); | |
| } | |
| response.cookie('auth', jwt, { | |
| domain: getCookieUrlFromDomain(process.env.FRONTEND_URL!), | |
| ...(!process.env.NOT_SECURED | |
| ? { | |
| secure: true, | |
| httpOnly: true, | |
| sameSite: 'none', | |
| } | |
| : {}), | |
| expires: new Date(Date.now() + 1000 * 60 * 60 * 24 * 365), | |
| }); | |
| if (process.env.NOT_SECURED) { | |
| response.header('auth', jwt); | |
| } | |
| response.header('reload', 'true'); | |
| response.status(200).json({ | |
| login: true, | |
| }); | |
| } | |
| } | |