-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize authentication module implementation with JWT strategy and…
… user registration
- Loading branch information
1 parent
95d2a58
commit ba4ef59
Showing
19 changed files
with
207 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
packages/server/prisma/migrations/20250109031805_initial/migration.sql
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import normalize from 'normalize-email'; | ||
|
||
export const normalizeEmail = (input: string) => { | ||
return normalize(input.toLowerCase().trim()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthController } from './auth.controller'; | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController], | ||
}).compile(); | ||
|
||
controller = module.get<AuthController>(AuthController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { RegisterDto } from './auth.dto'; | ||
import { AuthService } from './auth.service'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
|
||
@Post('register') | ||
async register(@Body() data: RegisterDto) { | ||
return this.authService.register(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { | ||
IsEmail, | ||
IsNotEmpty, | ||
IsOptional, | ||
IsString, | ||
MinLength, | ||
} from 'class-validator'; | ||
|
||
export class RegisterDto { | ||
@IsString() | ||
@IsNotEmpty() | ||
@MinLength(3) | ||
fullName: string; | ||
|
||
@IsEmail() | ||
@IsNotEmpty() | ||
email: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
password?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface TokenResponse { | ||
accessToken: string; | ||
refreshToken: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthController } from './auth.controller'; | ||
import { SupabaseService } from '../../providers/supabase/supabase.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { SupabaseModule } from '@/providers/supabase/supabase.module'; | ||
import { PrismaService } from '@/providers/prisma/prisma.service'; | ||
import { PrismaModule } from '@/providers/prisma/prisma.modules'; | ||
|
||
@Module({ | ||
imports: [ | ||
PassportModule.register({ defaultStrategy: 'jwt' }), | ||
SupabaseModule, | ||
ConfigModule, | ||
PrismaModule, | ||
], | ||
providers: [AuthService, JwtStrategy], | ||
controllers: [AuthController], | ||
}) | ||
export class AuthModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AuthService } from './auth.service'; | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService], | ||
}).compile(); | ||
|
||
service = module.get<AuthService>(AuthService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { SupabaseService } from '@/providers/supabase/supabase.service'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { RegisterDto } from './auth.dto'; | ||
import { PrismaService } from '@/providers/prisma/prisma.service'; | ||
import { normalizeEmail } from '@/helpers/normalize-emal'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AuthUser } from '@supabase/supabase-js'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private readonly supabase: SupabaseService, | ||
private readonly prisma: PrismaService, | ||
private readonly configService: ConfigService, | ||
) {} | ||
|
||
async register(dto: RegisterDto): Promise<AuthUser> { | ||
const normalizedEmail = normalizeEmail(dto.email); | ||
const { data, error } = await this.supabase.client.auth.signUp({ | ||
email: normalizedEmail, | ||
password: dto.password, | ||
options: { | ||
data: { | ||
fullName: dto.fullName, | ||
}, | ||
}, | ||
}); | ||
|
||
return this.prisma.expose(data.user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('supabase') { | ||
constructor(private readonly reflector: Reflector) { | ||
super(); | ||
} | ||
|
||
public canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
return super.canActivate(context); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { AuthUser } from '@supabase/supabase-js'; | ||
import { Request } from 'express'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor(readonly configService: ConfigService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: configService.get<String>('security.jwtSecret'), | ||
ignoreExpiration: false, | ||
}); | ||
} | ||
|
||
async validate(user: AuthUser): Promise<AuthUser> { | ||
/* | ||
Passport automatically creates a `user` object and assigns it to the | ||
Request object as `request.user`. | ||
*/ | ||
return user; | ||
} | ||
|
||
async authenticate(request: Request): Promise<void> { | ||
super.authenticate(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters