Skip to content
This repository was archived by the owner on Jul 10, 2019. It is now read-only.

Commit cd59a8b

Browse files
committed
feat: add ACCESS_TOKEN_NAME and ACCESS_TOKEN_COOKIE_NAME of constants
1 parent d1eb99a commit cd59a8b

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

src/common/constants/api.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,17 @@ export const API_ENDPOINT = {
2525
}
2626

2727
/** アクセストークンヘッダ名 - API サーバーとのログインセッション用のトークン名 */
28-
export const ACCESS_TOKEN_NAME = 'access-token'
28+
export const ACCESS_TOKEN_NAME = 'x-authorization-code'
29+
30+
/** アクセストークン Cookie 名 - API サーバーからのトークンを保存する先の Cookie 名 */
31+
export const ACCESS_TOKEN_COOKIE_NAME = 'x-authorization-code'
2932

3033
/** HTTP ステータス */
3134
export const HTTP_STATUS = {
3235
/** OK */
3336
OK: 200,
3437
/** 認証が必要である */
35-
UNAUTHORIZED: 401
38+
UNAUTHORIZED: 401,
39+
/** サービス利用不可 */
40+
SERVICE_UNAVAILABLE: 503
3641
}

src/utilities/auth.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
1+
/**
2+
* @file Cookieのユーティリティを集めたクラス
3+
*/
4+
15
import Cookie from 'js-cookie'
6+
import { ACCESS_TOKEN_COOKIE_NAME } from '@/common/constants/'
27

8+
/**
9+
* token を cookie に保存します
10+
* @param token
11+
*/
312
export const setToken = (token: string): void => {
4-
Cookie.set('token', token)
13+
Cookie.set(ACCESS_TOKEN_COOKIE_NAME, token)
514
}
615

16+
/**
17+
* token を cookie から削除します
18+
*/
719
export const unsetToken = (): void => {
8-
Cookie.remove('token')
20+
Cookie.remove(ACCESS_TOKEN_COOKIE_NAME)
921
window.localStorage.setItem('logout', Date.now() + '')
1022
}
1123

24+
/**
25+
* token を cookie から取得します
26+
* SSR 時はリクエストヘッダーから、 CSR 時は cookie から取得します
27+
* @param req
28+
*/
1229
export const getTokenFromCookie = (req?: any): string | undefined => {
1330
// SSR
1431
if (req && req.headers.cookie) {
1532
console.log('req.headers.cookie:', req.headers.cookie)
1633
const cookie: string = req.headers.cookie
1734
.split(';')
18-
.find((c: string): boolean => c.trim().startsWith('token='))
35+
.find(
36+
(c: string): boolean =>
37+
c.trim().startsWith(`${ACCESS_TOKEN_COOKIE_NAME}=`)
38+
)
1939

2040
if (!cookie) {
2141
return undefined
2242
}
2343

2444
const token = cookie.split('=')[1]
25-
return token
45+
// NOTE: req.headers.cookie から抜き取った cookie は URL エンコードされているため、スペースが %20 にエンコードされているので、デコードする
46+
return decodeURIComponent(token)
2647
}
2748

2849
// CSR
29-
return Cookie.get('token')
50+
return Cookie.get(ACCESS_TOKEN_COOKIE_NAME)
3051
}

tools/server.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const express = require('express')
22
const app = express()
33

4+
const ACCESS_TOKEN_NAME = 'x-authorization-code'
5+
46
// https://blog.ryo4004.net/web/306/
57
// method: post のために必須
68
const bodyParser = require('body-parser')
@@ -16,13 +18,16 @@ app.use((req, res, next) => {
1618
'Access-Control-Allow-Headers',
1719
// Chrome は OK で、 Firefox と IE11 がダメだったため、
1820
// '*' だと CORS 的に許可されないので、明示的にリクエストヘッダーの key 名を許可しています
19-
'origin, x-requested-with, content-type, accept, post-header, common-header, header1, access-token, X-User-Agent, X-Referer'
21+
`origin, x-requested-with, content-type, accept, post-header, common-header, header1, ${ACCESS_TOKEN_NAME}, X-User-Agent, X-Referer`
2022
// '*'
2123
)
2224
// https://stackoverflow.com/questions/37897523/axios-get-access-to-response-header-fields
2325
// https://github.com/axios/axios/issues/606
2426
// Access-Control-Expose-Headers を追加しないとカスタムレスポンスヘッダーをブラウザに返すことはできない
25-
res.header('Access-Control-Expose-Headers', 'from-server, access-token')
27+
res.header(
28+
'Access-Control-Expose-Headers',
29+
`from-server, ${ACCESS_TOKEN_NAME}`
30+
)
2631
next()
2732
})
2833

@@ -112,7 +117,7 @@ app.post('/login', (req, res) => {
112117
const id = uuid()
113118
console.log('uuid:', id)
114119
res.set({
115-
'access-token': id
120+
[ACCESS_TOKEN_NAME]: id
116121
})
117122

118123
res.send(
@@ -132,7 +137,7 @@ app.post('/logout', (req, res) => {
132137

133138
// カスタムレスポンスヘッダーをセットします
134139
res.set({
135-
'access-token': ''
140+
[ACCESS_TOKEN_NAME]: ''
136141
})
137142

138143
res.send(
@@ -146,7 +151,7 @@ app.post('/logout', (req, res) => {
146151
* post '/login-check'
147152
*/
148153
app.post('/login-check', (req, res) => {
149-
if (!req.headers['access-token']) {
154+
if (!req.headers[ACCESS_TOKEN_NAME]) {
150155
console.log('not login')
151156
res.send(
152157
JSON.stringify({
@@ -162,7 +167,7 @@ app.post('/login-check', (req, res) => {
162167
const id = uuid()
163168
console.log('uuid:', id)
164169
res.set({
165-
'access-token': id
170+
[ACCESS_TOKEN_NAME]: id
166171
})
167172

168173
res.send(

0 commit comments

Comments
 (0)