Skip to content

Commit

Permalink
rebuild authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
pengyu committed Feb 4, 2025
1 parent 55ee0b8 commit d0702f5
Show file tree
Hide file tree
Showing 31 changed files with 15,487 additions and 12,349 deletions.
Binary file modified backend/database.db
Binary file not shown.
3 changes: 2 additions & 1 deletion backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { AuthService } from './auth.service';
import { User } from 'src/user/user.model';
import { AuthResolver } from './auth.resolver';
import { JwtCacheService } from 'src/auth/jwt-cache.service';
import { RefreshToken } from './refresh-token/refresh-token.model';

@Module({
imports: [
ConfigModule,
TypeOrmModule.forFeature([Role, Menu, User]),
TypeOrmModule.forFeature([Role, Menu, User, RefreshToken]),
JwtModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
Expand Down
15 changes: 14 additions & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export class AuthService {
private menuRepository: Repository<Menu>,
@InjectRepository(Role)
private roleRepository: Repository<Role>,
@InjectRepository(RefreshToken)
private refreshTokenRepository: Repository<RefreshToken>,
) {}

async register(registerUserInput: RegisterUserInput): Promise<User> {
Expand Down Expand Up @@ -76,7 +78,18 @@ export class AuthService {
const accessToken = this.jwtService.sign(payload);
this.jwtCacheService.storeToken(accessToken);

return { accessToken };
const refreshToken = this.jwtService.sign(payload, {
expiresIn: '1d',
});

const refreshTokenEntity = this.refreshTokenRepository.create({
token: refreshToken,
userId: user.id,
expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
});
await this.refreshTokenRepository.save(refreshTokenEntity);

return { accessToken, refreshToken };
}

async validateToken(params: CheckTokenInput): Promise<boolean> {
Expand Down
19 changes: 19 additions & 0 deletions backend/src/auth/entities/refresh-token.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm';

@Entity()
export class RefreshToken {
@PrimaryGeneratedColumn()
id: number;

@Column()
token: string;

@Column()
userId: number;

@Column()
expiresAt: Date;

@CreateDateColumn()
createdAt: Date;
}
1 change: 1 addition & 0 deletions backend/src/user/dto/login-user.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export class LoginUserInput {
@IsString()
@MinLength(6)
password: string;

}
Empty file added frontend/apollo.config.json
Empty file.
1 change: 1 addition & 0 deletions frontend/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const nextConfig = {
// !! WARN !!
ignoreBuildErrors: true,
},

};

export default nextConfig;
Binary file added frontend/public/images/github.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added frontend/public/images/google.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
140 changes: 0 additions & 140 deletions frontend/src/app/(auth)/login/page.tsx

This file was deleted.

Loading

0 comments on commit d0702f5

Please sign in to comment.