-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
614 additions
and
32 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
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,13 @@ | ||
import { Migration } from '@mikro-orm/migrations'; | ||
|
||
export class Migration20241116145833 extends Migration { | ||
|
||
override async up(): Promise<void> { | ||
this.addSql(`alter table "user" add column "password" varchar(255) not null default 'changeme';`); | ||
} | ||
|
||
override async down(): Promise<void> { | ||
this.addSql(`alter table "user" drop column "password";`); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,12 +1,48 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { Controller, Get, Post, Request, UseGuards } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
import { LocalAuthGuard } from './auth/local-auth.guard'; | ||
import { AuthService } from './auth/auth.service'; | ||
import { GoogleStrategy } from './auth/google.strategy'; | ||
import { GoogleAuthGuard } from './auth/google-auth.guard'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
constructor( | ||
private readonly appService: AppService, | ||
private authService: AuthService, | ||
private googleStrategy: GoogleStrategy, | ||
) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
} | ||
|
||
@UseGuards(LocalAuthGuard) | ||
@Post('auth/login') | ||
async login(@Request() req) { | ||
return this.authService.login(req.user); | ||
} | ||
|
||
@UseGuards(LocalAuthGuard) | ||
@Post('auth/logout') | ||
async logout(@Request() req) { | ||
return req.logout(); | ||
} | ||
|
||
@UseGuards(GoogleAuthGuard) | ||
@Get('auth/google-login') | ||
async googleLogin(@Request() req) { | ||
console.log('request', req); | ||
return this.googleStrategy.authenticate(req); | ||
} | ||
|
||
@UseGuards(GoogleAuthGuard) | ||
@Get('auth/google/google-redirect') | ||
async googleLoginCallback(@Request() req) { | ||
console.log(req); | ||
console.log('REDIrect'); | ||
return req.user; | ||
// return this.googleStrategy.validate(req); | ||
} | ||
} |
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,24 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { UsersModule } from '../users/users.module'; | ||
import { LocalStrategy } from './local.strategy'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { jwtConstants } from './constants'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
import { GoogleStrategy } from './google.strategy'; | ||
|
||
|
||
@Module({ | ||
imports: [ | ||
UsersModule, | ||
PassportModule, | ||
JwtModule.register({ | ||
secret: jwtConstants.secret, | ||
signOptions: { expiresIn: '60s' }, | ||
}), | ||
], | ||
providers: [AuthService, LocalStrategy, JwtStrategy, GoogleStrategy], | ||
exports: [AuthService, GoogleStrategy], | ||
}) | ||
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,54 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UsersService } from '../users/users.service'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private usersService: UsersService, | ||
private jwtService: JwtService, | ||
) {} | ||
|
||
async validateUser(username: string, pass: string): Promise<any> { | ||
const user = await this.usersService.findByName(username); | ||
if (user && user.password === pass) { | ||
const { password, ...result } = user; | ||
return result; | ||
} | ||
return null; | ||
} | ||
//Реализация авторизации через google | ||
async validateGoogleUser(accessToken: string, refreshToken: string, profile: any) { | ||
const { name, emails, id, photos } = profile; | ||
const user = await this.usersService.findByName(emails[0].value); | ||
if (user) { | ||
return user; | ||
} | ||
|
||
const user_tmp = { | ||
email: emails[0].value, | ||
firstName: name.givenName, | ||
lastName: name.familyName, | ||
picture: photos[0].value, | ||
tmp: 'user authenticated', | ||
accessToken, | ||
refreshToken, | ||
}; | ||
|
||
const newUser = await this.usersService.create({ | ||
username: name, | ||
email: emails[0].value, | ||
password: id, | ||
}); | ||
|
||
return user_tmp; | ||
} | ||
|
||
|
||
async login(user: any) { | ||
const payload = { username: user.username, sub: user.userId }; | ||
return { | ||
access_token: this.jwtService.sign(payload), | ||
}; | ||
} | ||
} |
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 const jwtConstants = { | ||
secret: | ||
}; |
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,6 @@ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class GoogleAuthGuard extends AuthGuard('google') {} |
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,7 @@ | ||
|
||
export const googleAuthConfig = { | ||
callbackURL: 'http://localhost:4000/api/auth/google/google-redirect', | ||
passReqToCallback: true, | ||
scope: ['email', 'profile'] | ||
}; | ||
|
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 { OAuth2Strategy } from 'passport-google-oauth'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthService } from './auth.service'; | ||
import { googleAuthConfig } from './google-auth'; | ||
|
||
@Injectable() | ||
export class GoogleStrategy extends PassportStrategy(OAuth2Strategy) { | ||
constructor(private authService: AuthService) { | ||
super( | ||
googleAuthConfig | ||
); | ||
} | ||
|
||
async validate(req: any, accessToken: string, refreshToken: string, profile: any, done: Function) { | ||
const user = await this.authService.validateGoogleUser(accessToken, refreshToken, profile); | ||
|
||
|
||
done(null, 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,6 @@ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtAuthGuard extends AuthGuard('jwt') {} |
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,20 @@ | ||
|
||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { jwtConstants } from './constants'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: jwtConstants.secret, | ||
}); | ||
} | ||
|
||
async validate(payload: any) { | ||
return { userId: payload.sub, username: payload.username }; | ||
} | ||
} |
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,6 @@ | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class LocalAuthGuard extends AuthGuard('local') {} |
Oops, something went wrong.