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

Commit 9a02b24

Browse files
committed
feat: add login logout logincheck
1 parent 9361608 commit 9a02b24

File tree

9 files changed

+312
-32
lines changed

9 files changed

+312
-32
lines changed

src/common/constants/api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Env from '@/common/env/'
2+
3+
const getBffUrl = (path: string): string => [Env.url, path].join('')
4+
5+
export const API_ENDPOINT = {
6+
/** ログイン状態チェック */
7+
LOGIN_CHECK: getBffUrl('/login-check'),
8+
/** ログイン */
9+
LOGIN: getBffUrl('/login'),
10+
/** ログアウト */
11+
LOGOUT: getBffUrl('/logout')
12+
}

src/common/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ console.log('process.env.NODE_ENV: ', process.env.NODE_ENV)
66
console.log('RUNTIME_ENV: ', process.env.RUNTIME_ENV)
77

88
export * from './pjName'
9+
export * from './api'

src/interface/User/ILogin.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Login インターフェイス
3+
*/
4+
export interface ILoginPayload {
5+
username: string
6+
password: string
7+
}
8+
9+
export interface IUser {
10+
loggedIn: boolean
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* LoginCheck インターフェイス
3+
*/
4+
export interface ILoginCheckPayload {
5+
token: string
6+
}
7+
8+
export interface ILoginCheck {
9+
loggedIn: boolean
10+
}

src/interface/User/ILogout.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Logout インターフェイス
3+
*/
4+
export interface ILogoutPayload {
5+
token: string
6+
}
7+
8+
export interface ILogout {
9+
loggedIn: boolean
10+
}

src/store/auth.ts

Lines changed: 157 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
import Vue from 'vue'
2+
import { AxiosRequestConfig } from 'axios'
3+
import { ILoginPayload, IUser } from '@/interface/User/ILogin'
4+
import { ILoginCheckPayload, ILoginCheck } from '@/interface/User/ILoginCheck'
5+
import { ILogoutPayload, ILogout } from '@/interface/User/ILogout'
6+
import { setToken, unsetToken, cancelToken } from '@/utilities/'
27

38
/**
49
* store 用インターフェイス
510
*/
6-
export interface StateInterface {
11+
export interface IState {
12+
/**
13+
* ユーザー情報
14+
*/
15+
authUser: IUser | null
16+
/**
17+
* ログイン中かどうか
18+
*/
719
loggedIn: boolean
20+
/**
21+
* ローディング
22+
*/
23+
busy: {
24+
register: boolean
25+
login: boolean
26+
loginCheck: boolean
27+
logout: boolean
28+
}
829
}
930

1031
/**
1132
* state
1233
*/
13-
export const state = (): StateInterface => ({
14-
loggedIn: false
34+
export const state = (): IState => ({
35+
authUser: null,
36+
loggedIn: false,
37+
busy: {
38+
register: false,
39+
login: false,
40+
loginCheck: false,
41+
logout: false
42+
}
1543
})
1644

1745
/**
1846
* getters
1947
*/
2048
export const getters = {
21-
isAuthenticated(state: StateInterface) {
49+
isAuthenticated(state: IState): boolean {
2250
return !!state.loggedIn
2351
}
2452
}
@@ -27,11 +55,28 @@ export const getters = {
2755
* mutations
2856
*/
2957
export const mutations = {
58+
/**
59+
* ユーザー情報を更新する
60+
*/
61+
SET_USER: function(state: IState, user: IUser): void {
62+
state.authUser = user
63+
},
3064
/**
3165
* ログイン状態を更新する
3266
*/
33-
updateLoginStatus(state: StateInterface, status: boolean): void {
67+
updateLoginStatus(state: IState, status: boolean): void {
3468
state.loggedIn = status
69+
},
70+
/**
71+
* 処理中ステータスを更新する
72+
*/
73+
updateBusyStatus(
74+
state: IState,
75+
payload: [keyof IState['busy'], boolean]
76+
): void {
77+
const [key, status] = payload
78+
79+
Vue.set(state.busy, key, status)
3580
}
3681
}
3782

@@ -49,17 +94,49 @@ export const actions = {
4994
this: Vue,
5095
// @ts-ignore
5196
{ state, commit }: any,
52-
token: string
53-
): Promise<void> {
54-
console.log('login:', token === 'hisasann')
97+
payload: ILoginPayload
98+
): Promise<IUser | void> {
99+
console.log('login payload:', payload)
100+
101+
// ログイン中、またはすでにログイン済みなら処理を抜ける
102+
if (state.busy.login || state.loggedIn) {
103+
return
104+
}
105+
106+
// TODO: payload の validation はここかな
107+
108+
commit('updateBusyStatus', ['login', true])
55109

56110
try {
57-
// TODO サーバーにログイン
111+
const {
112+
data,
113+
headers,
114+
status,
115+
statusText,
116+
config
117+
} = await this.$axios.post<IUser>(
118+
this.$C.API_ENDPOINT.LOGIN,
119+
{
120+
username: payload.username,
121+
password: payload.password
122+
},
123+
{
124+
cancelToken: cancelToken.getToken(payload)
125+
} as AxiosRequestConfig
126+
)
127+
128+
setToken(headers['access-token'])
58129

59-
// 値をストアに保存
60-
await commit('updateLoginStatus', token === 'hisasann')
130+
// ログイン状態を更新
131+
commit('updateLoginStatus', data.loggedIn)
132+
// ユーザー情報をストアに保存
133+
commit('SET_USER', data)
134+
135+
return data
61136
} catch (err) {
62-
console.log(err)
137+
throw new Error(err)
138+
} finally {
139+
commit('updateBusyStatus', ['login', false])
63140
}
64141
},
65142

@@ -71,17 +148,43 @@ export const actions = {
71148
async logout(
72149
this: Vue,
73150
// @ts-ignore
74-
{ state, commit }: any
75-
): Promise<void> {
151+
{ state, commit }: any,
152+
payload: ILogoutPayload
153+
): Promise<ILogout | void> {
76154
console.log('logout')
77155

156+
// 処理中、未ログインなら中断
157+
if (state.busy.logout) {
158+
return
159+
}
160+
161+
commit('updateBusyStatus', ['logout', true])
162+
78163
try {
79-
// TODO サーバーからログアウト
164+
const token: string = payload.token
165+
const {
166+
data,
167+
headers,
168+
status,
169+
statusText,
170+
config
171+
} = await this.$axios.post<ILogout>(this.$C.API_ENDPOINT.LOGOUT, {}, {
172+
headers: {
173+
'access-token': token
174+
},
175+
cancelToken: cancelToken.getToken(payload)
176+
} as AxiosRequestConfig)
177+
178+
unsetToken()
80179

81-
// 値をストアに保存
82-
await commit('updateLoginStatus', false)
180+
// ログイン状態を更新
181+
commit('updateLoginStatus', data.loggedIn)
182+
183+
return data
83184
} catch (err) {
84-
console.log(err)
185+
throw new Error(err)
186+
} finally {
187+
commit('updateBusyStatus', ['logout', false])
85188
}
86189
},
87190

@@ -95,17 +198,46 @@ export const actions = {
95198
this: Vue,
96199
// @ts-ignore
97200
{ state, commit }: any,
98-
token: string
99-
): Promise<void> {
100-
console.log('loginCheck:', token === 'hisasann')
201+
payload: ILoginCheckPayload
202+
): Promise<ILoginCheck | void> {
203+
console.log('loginCheck', payload)
204+
205+
commit('updateBusyStatus', ['loginCheck', true])
101206

102207
try {
103-
// TODO ログインチェックとは?
208+
const postHeaders = {}
209+
const token: string | undefined = payload.token
210+
if (token) {
211+
postHeaders['access-token'] = token
212+
}
213+
214+
const {
215+
data,
216+
headers,
217+
status,
218+
statusText,
219+
config
220+
} = await this.$axios.post<ILoginCheck>(
221+
this.$C.API_ENDPOINT.LOGIN_CHECK,
222+
{},
223+
{
224+
headers: postHeaders,
225+
cancelToken: cancelToken.getToken(payload)
226+
} as AxiosRequestConfig
227+
)
228+
229+
if (headers['access-token'] && data.loggedIn) {
230+
setToken(headers['access-token'])
231+
}
232+
233+
// ログイン状態を更新
234+
commit('updateLoginStatus', data.loggedIn)
104235

105-
// 値をストアに保存
106-
await commit('updateLoginStatus', token === 'hisasann')
236+
return data
107237
} catch (err) {
108-
console.log(err)
238+
throw new Error(err)
239+
} finally {
240+
commit('updateBusyStatus', ['loginCheck', false])
109241
}
110242
}
111243
}

src/store/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { ActionContext } from 'vuex'
2+
import { ILoginCheckPayload } from '@/interface/User/ILoginCheck'
3+
import { getTokenFromCookie } from '@/utilities/'
24

35
/**
46
* store 用インターフェイス
@@ -45,7 +47,9 @@ export const mutations = {
4547
* actions
4648
*/
4749
export const actions = {
48-
/** サーバー初期化時の処理 */
50+
/**
51+
* サーバー初期化時の処理
52+
*/
4953
async nuxtServerInit(
5054
// @ts-ignore
5155
{ dispatch, commit, state }: ActionContext<any, any>,
@@ -54,8 +58,16 @@ export const actions = {
5458
): Promise<void> {
5559
await console.log('nuxtServerInit')
5660
commit('setIsServerInitCalled')
61+
62+
const token = getTokenFromCookie(req)
63+
64+
await dispatch('auth/loginCheck', {
65+
token
66+
} as ILoginCheckPayload)
5767
},
58-
/** クライアント初期化時の処理 */
68+
/**
69+
* クライアント初期化時の処理
70+
*/
5971
// @ts-ignore
6072
nuxtClientInit({ commit }: ActionContext<any, any>, { app }): void {
6173
console.log('nuxtClientInit')

src/utilities/auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const unsetToken = (): void => {
1010
}
1111

1212
// @ts-ignore
13-
export const getTokenFromCookie = (req: any): string | undefined => {
13+
export const getTokenFromCookie = (req?: any): string | undefined => {
1414
// SSR
1515
if (req && req.headers.cookie) {
1616
console.log('req.headers.cookie:', req.headers.cookie)
@@ -19,7 +19,7 @@ export const getTokenFromCookie = (req: any): string | undefined => {
1919
.find((c: string) => c.trim().startsWith('token='))
2020

2121
if (!cookie) {
22-
return ''
22+
return undefined
2323
}
2424

2525
const token = cookie.split('=')[1]

0 commit comments

Comments
 (0)