File

src/auth/auth.controller.ts

Prefix

auth

Index

Methods

Methods

Async acceptUsageTerms
acceptUsageTerms(userId: number)
Decorators :
@Patch('/profile/accept-usage-terms')
@ApiOperation({operationId: 'acceptUsageTerms', summary: 'Accepts the usage terms and conditions for the authenticated user'})
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOkResponse({type: DbUser})
@ApiNotFoundResponse({description: 'Unable to accept usage terms for the user'})
Parameters :
Name Type Optional
userId number No
Returns : Promise<DbUser>
Async deleteUserAccount
deleteUserAccount(userId: number)
Decorators :
@Delete('/profile')
@ApiOperation({operationId: 'deleteUserAccount', summary: 'Deletes the authenticated user's account'})
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOkResponse({type: DbUser})
@ApiNotFoundResponse({description: 'Unable to delete user account'})
Parameters :
Name Type Optional
userId number No
Returns : Promise<DbUser>
Async getSession
getSession(user: SupabaseAuthUser)
Decorators :
@Get('/session')
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOperation({operationId: 'checkAuthSession', summary: 'Get authenticated session information'})
@ApiOkResponse({type: SupabaseAuthDto})
@HttpCode(HttpStatus.OK)
Parameters :
Name Type Optional
user SupabaseAuthUser No
Async getWaitlisted
getWaitlisted()
Decorators :
@Get('/waitlisted')
@ApiOperation({operationId: 'getWaitlisted', summary: 'Gets number of waitlisted users'})
@ApiOkResponse({type: WaitlistedUsersDto})
@ApiNotFoundResponse({description: 'Unable to get waitlisted users'})
Async postOnboarding
postOnboarding(userId: number, body: UserOnboardingDto)
Decorators :
@Post('/onboarding')
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOperation({operationId: 'postOnboarding', summary: 'Updates onboarding information for user'})
@ApiOkResponse({type: SupabaseAuthDto})
@ApiNotFoundResponse({description: 'Unable to update onboarding information for the user'})
Parameters :
Name Type Optional
userId number No
body UserOnboardingDto No
Returns : Promise<void>
Async postWaitlist
postWaitlist(userId: number)
Decorators :
@Post('/waitlist')
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOperation({operationId: 'postWaitlist', summary: 'Updates waitlist information for user'})
@ApiOkResponse({type: SupabaseAuthDto})
@ApiNotFoundResponse({description: 'Unable to update waitlist information for the user'})
Parameters :
Name Type Optional
userId number No
Returns : Promise<void>
Async updateEmailPreferencesForUserProfile
updateEmailPreferencesForUserProfile(userId: number, updateUserDto: UpdateUserEmailPreferencesDto)
Decorators :
@Patch('/profile/email')
@ApiOperation({operationId: 'updateEmailPreferencesForUserProfile', summary: 'Updates the email preferences for the authenticated user profile'})
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOkResponse({type: DbUser})
@ApiNotFoundResponse({description: 'Unable to update email preferences for the user profile'})
@ApiBody({type: UpdateUserEmailPreferencesDto})
Parameters :
Name Type Optional
userId number No
updateUserDto UpdateUserEmailPreferencesDto No
Returns : Promise<DbUser>
Async updateInterestsForUserProfile
updateInterestsForUserProfile(userId: number, updateUserDto: UpdateUserProfileInterestsDto)
Decorators :
@Patch('/profile/interests')
@ApiOperation({operationId: 'updateInterestsForUserProfile', summary: 'Updates the interests for the authenticated user profile'})
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOkResponse({type: DbUser})
@ApiNotFoundResponse({description: 'Unable to update interests for the user profile'})
@ApiBody({type: UpdateUserProfileInterestsDto})
Parameters :
Name Type Optional
userId number No
updateUserDto UpdateUserProfileInterestsDto No
Returns : Promise<DbUser>
Async updateProfileForUser
updateProfileForUser(userId: number, updateUserDto: UpdateUserDto)
Decorators :
@Patch('/profile')
@ApiOperation({operationId: 'updateProfileForUser', summary: 'Updates the profile for the authenticated user'})
@ApiBearerAuth()
@UseGuards(SupabaseGuard)
@ApiOkResponse({type: DbUser})
@ApiNotFoundResponse({description: 'Unable to update user profile'})
@ApiBody({type: UpdateUserDto})
Parameters :
Name Type Optional
userId number No
updateUserDto UpdateUserDto No
Returns : Promise<DbUser>
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Patch, Post, UseGuards } from "@nestjs/common";
import { ApiBearerAuth, ApiBody, ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiTags } from "@nestjs/swagger";
import { SupabaseAuthUser } from "nestjs-supabase-auth";
import { UserService } from "../user/services/user.service";
import { DbUser } from "../user/user.entity";
import { UpdateUserDto } from "../user/dtos/update-user.dto";
import { UpdateUserEmailPreferencesDto } from "../user/dtos/update-user-email-prefs.dto";
import { UpdateUserProfileInterestsDto } from "../user/dtos/update-user-interests.dto";
import { SupabaseAuthDto } from "./dtos/supabase-auth-response.dto";
import { User, UserId } from "./supabase.user.decorator";
import { SupabaseGuard } from "./supabase.guard";
import { UserOnboardingDto } from "./dtos/user-onboarding.dto";
import { WaitlistedUsersDto } from "./dtos/waitlisted.dto";

@Controller("auth")
@ApiTags("Authentication service")
export class AuthController {
  constructor(private userService: UserService) {}

  @Get("/session")
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOperation({
    operationId: "checkAuthSession",
    summary: "Get authenticated session information",
  })
  @ApiOkResponse({ type: SupabaseAuthDto })
  @HttpCode(HttpStatus.OK)
  async getSession(@User() user: SupabaseAuthUser): Promise<SupabaseAuthDto> {
    const {
      email: session_email,
      confirmed_at,
      last_sign_in_at,
      created_at,
      updated_at,
      user_metadata: { sub: id, user_name },
    } = user;

    let userProfile: Partial<SupabaseAuthDto> = {};

    // check/insert user
    try {
      // get user from public users table
      const {
        is_onboarded,
        is_waitlisted,
        name,
        bio,
        location,
        twitter_username,
        company,
        display_local_time,
        url,
        email,
        github_sponsors_url,
        linkedin_url,
        discord_url,
        notification_count,
        coupon_code,
        insights_count,
        personal_workspace_id,
      } = await this.userService.checkAddUser(user);

      userProfile = {
        is_onboarded,
        is_waitlisted,
        name,
        location,
        bio,
        twitter_username,
        company,
        display_local_time,
        url,
        email,
        github_sponsors_url,
        linkedin_url,
        discord_url,
        notification_count,
        coupon_code,
        insights_count,
        personal_workspace_id,
      };
    } catch (e) {
      // leave user profile as-is
    }

    return {
      id: `${String(id)}`,
      user_name: `${String(user_name)}`,
      email: userProfile.email ?? session_email,
      confirmed_at,
      last_sign_in_at,
      created_at,
      updated_at,
      ...userProfile,
    };
  }

  @Post("/onboarding")
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOperation({
    operationId: "postOnboarding",
    summary: "Updates onboarding information for user",
  })
  @ApiOkResponse({ type: SupabaseAuthDto })
  @ApiNotFoundResponse({ description: "Unable to update onboarding information for the user" })
  async postOnboarding(@UserId() userId: number, @Body() body: UserOnboardingDto): Promise<void> {
    const userData = {
      timezone: body.timezone,
      interests: body.interests,
    };

    return this.userService.updateOnboarding(userId, userData);
  }

  @Post("/waitlist")
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOperation({
    operationId: "postWaitlist",
    summary: "Updates waitlist information for user",
  })
  @ApiOkResponse({ type: SupabaseAuthDto })
  @ApiNotFoundResponse({ description: "Unable to update waitlist information for the user" })
  async postWaitlist(@UserId() userId: number): Promise<void> {
    return this.userService.updateWaitlistStatus(userId);
  }

  @Get("/waitlisted")
  @ApiOperation({
    operationId: "getWaitlisted",
    summary: "Gets number of waitlisted users",
  })
  @ApiOkResponse({ type: WaitlistedUsersDto })
  @ApiNotFoundResponse({ description: "Unable to get waitlisted users" })
  async getWaitlisted(): Promise<WaitlistedUsersDto> {
    return this.userService.getNumWaitlisted();
  }

  @Patch("/profile")
  @ApiOperation({
    operationId: "updateProfileForUser",
    summary: "Updates the profile for the authenticated user",
  })
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOkResponse({ type: DbUser })
  @ApiNotFoundResponse({ description: "Unable to update user profile" })
  @ApiBody({ type: UpdateUserDto })
  async updateProfileForUser(@UserId() userId: number, @Body() updateUserDto: UpdateUserDto): Promise<DbUser> {
    return this.userService.updateUser(userId, updateUserDto);
  }

  @Patch("/profile/interests")
  @ApiOperation({
    operationId: "updateInterestsForUserProfile",
    summary: "Updates the interests for the authenticated user profile",
  })
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOkResponse({ type: DbUser })
  @ApiNotFoundResponse({ description: "Unable to update interests for the user profile" })
  @ApiBody({ type: UpdateUserProfileInterestsDto })
  async updateInterestsForUserProfile(
    @UserId() userId: number,
    @Body() updateUserDto: UpdateUserProfileInterestsDto
  ): Promise<DbUser> {
    await this.userService.updateInterests(userId, updateUserDto);

    return this.userService.tryFindUserOrMakeStub({ userId });
  }

  @Patch("/profile/email")
  @ApiOperation({
    operationId: "updateEmailPreferencesForUserProfile",
    summary: "Updates the email preferences for the authenticated user profile",
  })
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOkResponse({ type: DbUser })
  @ApiNotFoundResponse({ description: "Unable to update email preferences for the user profile" })
  @ApiBody({ type: UpdateUserEmailPreferencesDto })
  async updateEmailPreferencesForUserProfile(
    @UserId() userId: number,
    @Body() updateUserDto: UpdateUserEmailPreferencesDto
  ): Promise<DbUser> {
    await this.userService.updateEmailPreferences(userId, updateUserDto);

    return this.userService.tryFindUserOrMakeStub({ userId });
  }

  @Delete("/profile")
  @ApiOperation({
    operationId: "deleteUserAccount",
    summary: "Deletes the authenticated user's account",
  })
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOkResponse({ type: DbUser })
  @ApiNotFoundResponse({ description: "Unable to delete user account" })
  async deleteUserAccount(@UserId() userId: number): Promise<DbUser> {
    return this.userService.deleteUser(userId);
  }

  @Patch("/profile/accept-usage-terms")
  @ApiOperation({
    operationId: "acceptUsageTerms",
    summary: "Accepts the usage terms and conditions for the authenticated user",
  })
  @ApiBearerAuth()
  @UseGuards(SupabaseGuard)
  @ApiOkResponse({ type: DbUser })
  @ApiNotFoundResponse({ description: "Unable to accept usage terms for the user" })
  async acceptUsageTerms(@UserId() userId: number): Promise<DbUser> {
    await this.userService.acceptTerms(userId);

    return this.userService.tryFindUserOrMakeStub({ userId });
  }
}

results matching ""

    No results matching ""