|
| 1 | +import { |
| 2 | + Injectable, |
| 3 | + BadRequestException, |
| 4 | + UnauthorizedException, |
| 5 | +} from '@nestjs/common'; |
| 6 | +import { SupabaseService } from '../supabase/supabase.service'; |
| 7 | +import { SignInDto, SignUpDto } from '@repo/dtos/auth'; |
| 8 | +import { UserDetails } from 'src/supabase/collection'; |
| 9 | + |
| 10 | +@Injectable() |
| 11 | +export class AuthService { |
| 12 | + constructor(private readonly supabaseService: SupabaseService) {} |
| 13 | + private readonly PROFILES_TABLE = 'profiles'; |
| 14 | + |
| 15 | + async me(token: string) { |
| 16 | + const { data, error } = await this.supabaseService |
| 17 | + .getClient() |
| 18 | + .auth.getUser(token); |
| 19 | + |
| 20 | + if (error) { |
| 21 | + throw new UnauthorizedException(error.message); |
| 22 | + } |
| 23 | + const { user } = data; |
| 24 | + if (!user || !data) { |
| 25 | + throw new BadRequestException('Unexpected sign-in response.'); |
| 26 | + } |
| 27 | + const { data: userData, error: profileError } = await this.supabaseService |
| 28 | + .getClient() |
| 29 | + .from(this.PROFILES_TABLE) |
| 30 | + .select(`id, email, username`) |
| 31 | + .eq('id', user.id) |
| 32 | + .returns<UserDetails[]>() |
| 33 | + .single(); |
| 34 | + if (profileError) { |
| 35 | + throw new BadRequestException(profileError.message); |
| 36 | + } |
| 37 | + return { userData }; |
| 38 | + } |
| 39 | + |
| 40 | + async signUp(signUpDto: SignUpDto) { |
| 41 | + const { email, password, username } = signUpDto; |
| 42 | + |
| 43 | + // Step 1: Create user in Supabase Auth |
| 44 | + const { data, error } = await this.supabaseService.getClient().auth.signUp({ |
| 45 | + email, |
| 46 | + password, |
| 47 | + options: { |
| 48 | + data: { |
| 49 | + username, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }); |
| 53 | + if (error) { |
| 54 | + throw new BadRequestException(error.message); |
| 55 | + } |
| 56 | + const { user, session } = data; |
| 57 | + |
| 58 | + if (!user || !session) { |
| 59 | + throw new BadRequestException('Unexpected error occured'); |
| 60 | + } |
| 61 | + |
| 62 | + // Step 2: Insert profile data into profiles table |
| 63 | + const { data: userData, error: profileError } = await this.supabaseService |
| 64 | + .getClient() |
| 65 | + .from(this.PROFILES_TABLE) |
| 66 | + .insert([ |
| 67 | + { |
| 68 | + id: user.id, |
| 69 | + username, |
| 70 | + email, |
| 71 | + }, |
| 72 | + ]) |
| 73 | + .returns<UserDetails[]>() |
| 74 | + .single(); |
| 75 | + |
| 76 | + if (profileError) { |
| 77 | + // Delete the created user if profile creation fails |
| 78 | + await this.supabaseService.getClient().auth.admin.deleteUser(user.id); |
| 79 | + throw new BadRequestException(profileError.message); |
| 80 | + } |
| 81 | + |
| 82 | + // Return user and session information |
| 83 | + return { userData, session }; |
| 84 | + } |
| 85 | + |
| 86 | + async signIn(signInDto: SignInDto) { |
| 87 | + const { email, password } = signInDto; |
| 88 | + const { data, error } = await this.supabaseService |
| 89 | + .getClient() |
| 90 | + .auth.signInWithPassword({ email, password }); |
| 91 | + |
| 92 | + if (error) { |
| 93 | + throw new BadRequestException(error.message); |
| 94 | + } |
| 95 | + const { user, session } = data; |
| 96 | + if (!user || !data) { |
| 97 | + throw new BadRequestException('Unexpected sign-in response.'); |
| 98 | + } |
| 99 | + |
| 100 | + const { data: userData, error: profileError } = await this.supabaseService |
| 101 | + .getClient() |
| 102 | + .from(this.PROFILES_TABLE) |
| 103 | + .select(`id, email, username`) |
| 104 | + .eq('id', user.id) |
| 105 | + .returns<UserDetails[]>() |
| 106 | + .single(); |
| 107 | + |
| 108 | + if (profileError) { |
| 109 | + throw new BadRequestException(profileError.message); |
| 110 | + } |
| 111 | + |
| 112 | + return { userData, session }; |
| 113 | + } |
| 114 | +} |
0 commit comments