반응형
Notice
Recent Posts
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- firebase-database
- 오즈뷰어
- gradle
- JNI
- git-push
- mqtt
- NoSuchMethodError
- Firebase
- google-login
- Kotlin
- hung-up
- socket.io
- ActivityResult-API
- BottomSheetDialog
- AWS
- Dva
- firebase-storage
- cloud-firestore
- mosquitto
- Android
- Galaxy Watch
- Java8
- OZViewer
- git
- socket-client
- socket-server
- 워치
- TIZEN
- Flavors
- ozd
Archives
- Today
- Total
Hyeyeon blog
[Node.js] Basic Auth 구현하기 (+ swagger 추가) 본문
반응형
Basic Auth 란?
API 호출 시, 헤더의 Authorization 값으로 user id와 password를 보내어 인증하는 방식입니다.
* Postman에서 아래와 같이 확인할 수 있습니다.
인증 확인하기
router.post('/login', (req, res) => {
const auth = { username: 'testName', password: "testPassword" } // 사용하고자 하는 계정 정보 입력
const base64Credentials = (req.headers.authorization || '').split(' ')[1] || ''
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii')
const [username, password] = credentials.split(':')
if (!username || !password || username != auth.username || password != auth.password) {
// 인증 실패
res.status(401).json({"status":"failed", "message" : "Authentication Error"}) return
} else {
// 인증 성공
}
}
Swagger 추가하기
1. swaggerDefinitions 설정
info: {
version: '1.0.0',
title: 'TITLE' },
securityDefinitions: {
basicAuth: {
type : 'basic'
}
}
2. swagger 주석
/**
* @swagger
* /auth/login:
* post:
* tags: [Auth]
* security:
* - basicAuth:
* type: basic
...
*/
728x90
'개발 > Node.js' 카테고리의 다른 글
[Node.js] Socket.io를 이용한 소켓 서버 구현하기 (0) | 2020.12.11 |
---|
Comments