-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FE][Feat] 로그인 api 연동
- Loading branch information
Showing
11 changed files
with
218 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,35 @@ | ||
{ | ||
"name": "ddara-backend", | ||
"private": true, | ||
"workspaces": [ | ||
"frontend", | ||
"backend" | ||
], | ||
"version": "0.0.0", | ||
"type": "module", | ||
"description": "따라따라의 선따라길따라 BackEnd 코드", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "node src/index.js", | ||
"test": "vitest", | ||
"test:watch": "vitest --watch", | ||
"test:coverage": "vitest run --coverage", | ||
"lint": "pnpm lint-staged" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"bcrypt": "^5.1.1", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.21.1", | ||
"express-validator": "^7.2.0", | ||
"jsonwebtoken": "^9.0.2", | ||
"pg": "^8.13.1", | ||
"swagger-jsdoc": "^6.2.8", | ||
"swagger-ui-express": "^5.0.1", | ||
"uuid": "^11.0.3", | ||
"ws": "^8.11.0" | ||
} | ||
} | ||
{ | ||
"name": "ddara-backend", | ||
"private": true, | ||
"workspaces": [ | ||
"frontend", | ||
"backend" | ||
], | ||
"version": "0.0.0", | ||
"type": "module", | ||
"description": "따라따라의 선따라길따라 BackEnd 코드", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "node src/index.js", | ||
"test": "vitest", | ||
"test:watch": "vitest --watch", | ||
"test:coverage": "vitest run --coverage", | ||
"lint": "pnpm lint-staged" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"bcrypt": "^5.1.1", | ||
"cors": "^2.8.5", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.21.1", | ||
"express-validator": "^7.2.0", | ||
"jsonwebtoken": "^9.0.2", | ||
"pg": "^8.13.1", | ||
"swagger-jsdoc": "^6.2.8", | ||
"swagger-ui-express": "^5.0.1", | ||
"uuid": "^11.0.3", | ||
"ws": "^8.11.0" | ||
} | ||
} |
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,27 @@ | ||
import { getApiClient } from '@/api/client.api.ts'; | ||
import { ResponseDto } from '@/api/dto/response.dto.ts'; | ||
import { LoginResEntity } from '@/api/dto/auth.dto.ts'; | ||
|
||
export const doLogin = (id: string, password: string): Promise<ResponseDto<LoginResEntity>> => { | ||
const promiseFn = ( | ||
fnResolve: (value: ResponseDto<LoginResEntity>) => void, | ||
fnReject: (reason?: any) => void, | ||
) => { | ||
const apiClient = getApiClient(); | ||
apiClient | ||
.post('/auth/login', { id, password }) | ||
.then(res => { | ||
if (!res.data.success) { | ||
console.error(res); | ||
fnReject(`msg.${res.data.resultMsg}`); | ||
} else { | ||
fnResolve(new ResponseDto<LoginResEntity>(res.data)); | ||
} | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
fnReject('msg.RESULT_FAILED'); | ||
}); | ||
}; | ||
return new Promise(promiseFn); | ||
}; |
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 { AppConfig } from '@/constants'; | ||
import { loadLocalData } from '@/utils/common/manageLocalData.ts'; | ||
import axios from 'axios'; | ||
|
||
let apiClient = axios.create({ | ||
baseURL: AppConfig.API_SERVER, | ||
headers: { | ||
'Content-type': 'application/json', | ||
'Access-Control-Allow-Origin': '*', | ||
'Access-Control-Allow-Credentials': true, | ||
}, | ||
}); | ||
|
||
export function setApiToken(token: string): void { | ||
if (apiClient) apiClient.defaults.headers.common.Authorization = `Bearer ${token}`; | ||
} | ||
|
||
export const getApiClient = () => { | ||
const token = loadLocalData(AppConfig.KEYS.LOGIN_TOKEN); | ||
if (token) setApiToken(token); | ||
if (!apiClient) { | ||
apiClient = axios.create({ | ||
baseURL: AppConfig.API_SERVER, | ||
headers: { | ||
'Content-type': 'application/json', | ||
'Access-Control-Allow-Origin': '*', | ||
}, | ||
}); | ||
} | ||
return apiClient; | ||
}; |
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 @@ | ||
export class LoginResEntity { | ||
token: string | undefined; | ||
|
||
userId: string | undefined; | ||
} |
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,16 @@ | ||
export class ResponseDto<T> { | ||
resultCode = 0; | ||
|
||
resultMsg = ''; | ||
|
||
data?: T; | ||
|
||
totalCount = 0; | ||
|
||
constructor(data?: any) { | ||
if (data.resultCode) this.resultCode = data.resultCode; | ||
if (data.resultMsg) this.resultMsg = data.resultMsg; | ||
if (data.data) this.data = data.data; | ||
if (data.totalCount) this.totalCount = data.totalCount; | ||
} | ||
} |
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,17 @@ | ||
export const KEYS = { | ||
LOGIN_USER: 'LUT', | ||
LOGIN_TOKEN: 'LUT_TK', | ||
}; | ||
|
||
export const AppConfig = { | ||
/** | ||
* API SERVER | ||
*/ | ||
API_SERVER: 'http://223.130.151.43:3001/api', | ||
// API_SERVER: 'http://localhost:3001/api', | ||
|
||
/** | ||
* ETC | ||
*/ | ||
KEYS, | ||
}; |
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,53 @@ | ||
/* eslint-disable */ | ||
import lzString from 'lz-string' | ||
|
||
export function saveLocalData(key: string, val: string): void { | ||
if (typeof window !== 'undefined') { | ||
const storage = window.localStorage; | ||
|
||
if (storage) { | ||
try { | ||
storage.setItem(key, lzString.compressToUTF16(val)); | ||
} catch (e) { | ||
console.error('Storage Full ... clean old data...'); | ||
for (const k in storage) { | ||
if (k.indexOf('DATA_MESSAGE_DETAIL_') > -1) { | ||
storage.removeItem(k); | ||
} | ||
} | ||
storage.setItem(key, lzString.compressToUTF16(val)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function loadLocalData(key: string): string | null { | ||
if (typeof window !== undefined) { | ||
const storage = window.localStorage; | ||
|
||
if (storage) { | ||
const keyValue = storage.getItem(key); | ||
if (keyValue) return lzString.decompressFromUTF16(keyValue); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
export function clearLocalData() { | ||
if (typeof window !== undefined) { | ||
const storage = window.localStorage; | ||
|
||
if (storage) { | ||
storage.clear(); | ||
} | ||
} | ||
} | ||
|
||
export function removeLocalData(key: string): void { | ||
if (typeof window !== undefined) { | ||
const storage = window.localStorage; | ||
if (storage) { | ||
storage.removeItem(key); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.