generated from kir-dev/next-nest-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/17-add-pagination-support
- Loading branch information
Showing
49 changed files
with
2,065 additions
and
955 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Contributing | ||
|
||
This application is being developed internally in our organization so the issues will be assigned to our members. If you're not a member but really want to help, look for issues with the 'help wanted' label. If we need outside help, we'll open an issue with that tag and describe our problem there. | ||
|
||
If you have a bug report or a feature request for the website, please open an issue and describe the feature/bug as thouroghly as possible. | ||
|
||
Thank you! |
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 +1,5 @@ | ||
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/mydb?schema=public" | ||
AUTHSCH_CLIENT_ID="<generate at auth.sch.bme.hu>" | ||
AUTHSCH_CLIENT_SECRET="<generate at auth.sch.bme.hu>" | ||
JWT_SECRET="very secret value" | ||
FRONTEND_URL="http://localhost:3000" |
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
18 changes: 0 additions & 18 deletions
18
apps/backend/prisma/migrations/20240509172402_comment_out_user/migration.sql
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
...a/migrations/20240519193007_favorite_drink_model_reolaced_with_many_to_many/migration.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { CurrentUser } from '@kir-dev/passport-authsch'; | ||
import { Controller, Get, Redirect, UseGuards } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
|
||
import { AuthService } from './auth.service'; | ||
|
||
@Controller('auth') | ||
export class AuthController { | ||
constructor(private authService: AuthService) {} | ||
/** | ||
* Redirects to the authsch login page | ||
*/ | ||
@UseGuards(AuthGuard('authsch')) | ||
@Get('login') | ||
login() { | ||
// never called | ||
} | ||
|
||
/** | ||
* Endpoint for authsch to call after login | ||
* Redirects to the frontend with the jwt token | ||
*/ | ||
@Get('callback') | ||
@UseGuards(AuthGuard('authsch')) | ||
@Redirect() | ||
oauthRedirect(@CurrentUser() user: User) { | ||
const jwt = this.authService.login(user); | ||
return { | ||
url: `${process.env.FRONTEND_URL}?jwt=${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,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
import { PassportModule } from '@nestjs/passport'; | ||
|
||
import { AuthController } from './auth.controller'; | ||
import { AuthService } from './auth.service'; | ||
import { AuthSchStrategy } from './authsch.strategy'; | ||
import { JwtStrategy } from './jwt.strategy'; | ||
|
||
@Module({ | ||
controllers: [AuthController], | ||
providers: [AuthService, AuthSchStrategy, JwtStrategy], | ||
imports: [PassportModule, JwtModule], | ||
}) | ||
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,33 @@ | ||
import { AuthSchProfile } from '@kir-dev/passport-authsch'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { PrismaService } from 'nestjs-prisma'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
|
||
@Injectable() | ||
export class AuthService { | ||
constructor( | ||
private prisma: PrismaService, | ||
private jwtService: JwtService | ||
) {} | ||
|
||
async findOrCreateUser(userProfile: AuthSchProfile): Promise<User> { | ||
const user = await this.prisma.user.findUnique({ where: { authSchId: userProfile.authSchId } }); | ||
if (user) return user; | ||
return await this.prisma.user.create({ | ||
data: { | ||
authSchId: userProfile.authSchId, | ||
firstName: userProfile.firstName, | ||
lastName: userProfile.lastName, | ||
email: userProfile.email, | ||
}, | ||
}); | ||
} | ||
|
||
login(user: User): string { | ||
return this.jwtService.sign(user, { | ||
secret: process.env.JWT_SECRET, | ||
expiresIn: '7 days', | ||
}); | ||
} | ||
} |
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,21 @@ | ||
import { AuthSchProfile, AuthSchScope, Strategy } from '@kir-dev/passport-authsch'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
|
||
import { AuthService } from './auth.service'; | ||
|
||
@Injectable() | ||
export class AuthSchStrategy extends PassportStrategy(Strategy) { | ||
constructor(private authService: AuthService) { | ||
super({ | ||
clientId: process.env.AUTHSCH_CLIENT_ID, | ||
clientSecret: process.env.AUTHSCH_CLIENT_SECRET, | ||
scopes: [AuthSchScope.BASIC, AuthSchScope.FIRST_NAME, AuthSchScope.LAST_NAME, AuthSchScope.EMAIL], | ||
}); | ||
} | ||
|
||
async validate(userProfile: AuthSchProfile): Promise<User> { | ||
return await this.authService.findOrCreateUser(userProfile); | ||
} | ||
} |
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,19 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
import { User } from 'src/users/entities/user.entity'; | ||
|
||
@Injectable() | ||
export class JwtStrategy extends PassportStrategy(Strategy) { | ||
constructor() { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: process.env.JWT_SECRET, | ||
}); | ||
} | ||
|
||
validate(payload: User): User { | ||
return payload; | ||
} | ||
} |
Oops, something went wrong.