Skip to content

Commit

Permalink
feat : 카카오 로그인
Browse files Browse the repository at this point in the history
  • Loading branch information
verdantjuly committed Mar 5, 2024
1 parent 2f0d707 commit 92a8d1d
Show file tree
Hide file tree
Showing 41 changed files with 11,611 additions and 891 deletions.
9,836 changes: 9,836 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/common": "^10.3.3",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.3",
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/typeorm": "^10.0.2",
"@types/passport-kakao": "^1.0.3",
"class-validator": "^0.14.1",
"dotenv": "^16.4.5",
"passport": "^0.7.0",
"passport-kakao": "^1.0.1",
"pg": "^8.11.3",
"postgres": "^3.4.3",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
Expand Down
17 changes: 17 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="startKakao()">Kakao Login</button>
<script src="./index.js"></script>
<script
src="https://t1.kakaocdn.net/kakao_js_sdk/2.3.0/kakao.min.js"
integrity="sha384-70k0rrouSYPWJt7q9rSTKpiTfX6USlMYjZUtr1Du+9o4cGvhPAWxngdtVZDdErlh"
crossorigin="anonymous"
></script>
</body>
</html>
20 changes: 20 additions & 0 deletions public/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function startKakao() {
try {
const response = await fetch(`./api/auth/kakaokey`, {
method: 'POST',
});
const result = await response.json();
console.log(result.kakaoJSKey);
console.log(result.kakaoRedirectURI);
await Kakao.init(result.kakaoJSKey);
await Kakao.Auth.authorize({
redirectUri: result.kakaoRedirectURI,
});
} catch (err) {
console.log(err);
// alert(
// '로그인이 정상적으로 완료되지 않았습니다. 새로고침 이후 다시 시도해 주세요.',
// );
}
}
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';

import dotenv from 'dotenv';

dotenv.config();

@Module({
imports: [
Expand All @@ -14,6 +19,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
entities: ['dist/**/**.entity{.ts,.js}'],
synchronize: true,
}),
AuthModule,
],
controllers: [],
providers: [],
Expand Down
14 changes: 0 additions & 14 deletions src/auth/adapter/in/auth.controller.ts

This file was deleted.

15 changes: 0 additions & 15 deletions src/auth/adapter/in/dto/signup.dto.ts

This file was deleted.

30 changes: 0 additions & 30 deletions src/auth/adapter/out/auth.entity.ts

This file was deleted.

Empty file.
54 changes: 54 additions & 0 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Module } from '@nestjs/common';
import { KakaoStrategy } from './strategies';
import { AuthController } from './controllers';
import { AuthService, UserService } from './services';
import {
AccessTokenRepository,
RefreshTokenRepository,
UserRepository,
} from './repositories';
import { TypeOrmModule } from '@nestjs/typeorm';
import {
AccessLog,
AccessToken,
RefreshToken,
TokenBlacklist,
User,
} from './entities';
import { JwtModule, JwtService } from '@nestjs/jwt';
import dotenv from 'dotenv';

dotenv.config();

@Module({
imports: [
JwtModule.register({ secret: process.env.JWT_SECRET }),
TypeOrmModule.forFeature([
User,
AccessToken,
RefreshToken,
AccessLog,
TokenBlacklist,
]),
],
controllers: [AuthController],
providers: [
UserRepository,
AccessTokenRepository,
RefreshTokenRepository,

KakaoStrategy,
UserService,
AuthService,
],
exports: [
UserRepository,
AccessTokenRepository,
RefreshTokenRepository,

KakaoStrategy,
UserService,
AuthService,
],
})
export class AuthModule {}
18 changes: 18 additions & 0 deletions src/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Controller, Get, Post, Req, UseGuards } from '@nestjs/common';
import { AuthService } from '../services';
import { AuthGuard } from '@nestjs/passport';

@Controller('api/auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Get('login/kakao')
@UseGuards(AuthGuard('kakao'))
async kakaoCallback(@Req() req) {
await this.authService.OAuthLogin(req);
}
@Post('kakaokey')
async kakaoKey() {
return await this.authService.kakaoKey();
}
}
1 change: 1 addition & 0 deletions src/auth/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './auth.controller';
9 changes: 0 additions & 9 deletions src/auth/domain/auth.service.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/auth/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class CreateUsserDto {}
1 change: 1 addition & 0 deletions src/auth/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './create-user.dto';
24 changes: 24 additions & 0 deletions src/auth/entities/access-log.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { BaseEntity } from 'src/common/enitty';
import { User } from './user.entity';

@Entity()
export class AccessLog extends BaseEntity {
@Column({ type: 'varchar', length: 512, nullable: true })
ua: string;

@Column()
endpoint: string;

@Column()
ip: string;

@Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
accessedAt: Date;

@ManyToOne(() => User, user => user.accessLogs, {
nullable: true,
onDelete: 'CASCADE',
})
user: User[];
}
18 changes: 18 additions & 0 deletions src/auth/entities/access-token.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Column, Entity, ManyToOne } from 'typeorm';
import { User } from './user.entity';
import { BaseEntity } from 'src/common/enitty';

@Entity()
export class AccessToken extends BaseEntity {
@ManyToOne(() => User, user => user.accessToken)
user: User;

@Column({ type: 'varchar' })
token: string;

@Column({ type: 'timestamp' })
expiresAt: Date;

@Column({ default: false })
isRevoked: boolean;
}
5 changes: 5 additions & 0 deletions src/auth/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './access-log.entity';
export * from './access-token.entity';
export * from './refresh-token.entity';
export * from './token-blacklist.entity';
export * from './user.entity';
18 changes: 18 additions & 0 deletions src/auth/entities/refresh-token.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Column, Entity, ManyToOne, PrimaryColumn } from 'typeorm';
import { User } from './user.entity';
import { BaseEntity } from 'src/common/enitty';

@Entity()
export class RefreshToken extends BaseEntity {
@ManyToOne(() => User, user => user.refreshToken)
user: User;

@PrimaryColumn()
token: string;

@Column({ type: 'timestamp' })
expiresAt: Date;

@Column({ default: false })
isRevoked: boolean;
}
14 changes: 14 additions & 0 deletions src/auth/entities/token-blacklist.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';
import { BaseEntity } from 'src/common/enitty';

@Entity()
export class TokenBlacklist extends BaseEntity {
@PrimaryColumn()
token: string;

@Column()
tokenType: 'access' | 'refresh';

@Column({ type: 'timestamp' })
expiresAt: Date;
}
30 changes: 30 additions & 0 deletions src/auth/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BaseEntity } from 'src/common/enitty';
import { Column, Entity, OneToMany } from 'typeorm';
import { UserRole } from '../types';
import { AccessToken } from './access-token.entity';
import { RefreshToken } from './refresh-token.entity';
import { AccessLog } from './access-log.entity';

@Entity()
export class User extends BaseEntity {
@Column({ type: 'varchar' })
name: string;

@Column({ type: 'varchar' })
password: string;

@Column({ type: 'varchar', length: 50, nullable: true })
phone: string;

@Column({ type: 'varchar', length: 30, default: 'user' })
role: UserRole;

@OneToMany(() => AccessToken, token => token.user)
accessToken: AccessToken[];

@OneToMany(() => RefreshToken, token => token.user)
refreshToken: RefreshToken[];

@OneToMany(() => AccessLog, log => log.user)
accessLogs: AccessLog[];
}
1 change: 1 addition & 0 deletions src/auth/guard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './kakaoauth.guard';
5 changes: 5 additions & 0 deletions src/auth/guard/kakaoauth.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class KakaoAuthGuard extends AuthGuard('kakao') {}
5 changes: 0 additions & 5 deletions src/auth/port/in/auth.useCase.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/auth/port/out/auth.port.ts

This file was deleted.

Loading

0 comments on commit 92a8d1d

Please sign in to comment.