Skip to content

Commit f17ba5f

Browse files
committed
FIX: AuthのCookie上の維持
1 parent ea399fc commit f17ba5f

File tree

9 files changed

+168
-28
lines changed

9 files changed

+168
-28
lines changed

src/helpers/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ export function getUserFromCookie(req) {
1818
}
1919

2020
export function getUserFromSession(req) {
21-
console.log('[CHECK-AUTH] - checking if user is stored in session')
2221
return req.session ? req.session.userId : null
2322
}

src/layouts/protected.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
import { mapActions } from 'vuex'
5757
export default {
5858
middleware: 'authenticated',
59+
data() {
60+
return {
61+
loading: true,
62+
}
63+
},
64+
mounted() {
65+
this.loading = false
66+
},
5967
methods: {
6068
...mapActions('modules/user', ['logout']),
6169
async signout() {
@@ -68,14 +76,6 @@ export default {
6876
// })
6977
},
7078
},
71-
data() {
72-
return {
73-
loading: true,
74-
}
75-
},
76-
mounted() {
77-
this.loading = false
78-
},
7979
}
8080
</script>
8181

src/middleware/authenticated.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
import firebase from '@/plugins/firebase'
2-
3-
export default function ({ store, route, redirect }) {
4-
firebase.auth().onAuthStateChanged((user) => {
5-
if (!user) {
6-
redirect('/account/login')
7-
} else {
8-
store.commit('user/setUser', {
9-
uid: user.uid,
10-
email: user.email,
11-
username: user.displayName,
12-
userImage: user.photoURL,
13-
})
14-
}
15-
})
1+
export default function ({ store, redirect }) {
2+
if (!store.getters['modules/user/isAuthenticated']) {
3+
return redirect('/account/login')
4+
}
165
}

src/pages/account/login.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
</template>
4747

4848
<script>
49+
import { mapActions } from 'vuex'
4950
import firebase from '@/plugins/firebase'
5051
export default {
5152
data() {
@@ -56,11 +57,13 @@ export default {
5657
}
5758
},
5859
methods: {
59-
doLogin() {
60-
firebase
60+
...mapActions('modules/user', ['login']),
61+
async doLogin() {
62+
await firebase
6163
.auth()
6264
.signInWithEmailAndPassword(this.email, this.password)
6365
.then((user) => {
66+
this.login(user)
6467
this.$router.push('/edit')
6568
})
6669
.catch((error) => {

src/pages/account/signup.vue

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<template>
2+
<div>
3+
<v-container fluid fill-height>
4+
<v-layout align-center justify-center>
5+
<v-flex xs12 sm8 md4>
6+
<v-card>
7+
<v-toolbar>
8+
<v-toolbar-title>Login</v-toolbar-title>
9+
</v-toolbar>
10+
<v-card-text>
11+
<v-form>
12+
<v-text-field
13+
v-model="email"
14+
:counter="32"
15+
label="email"
16+
prepend-icon="mdi-email"
17+
/>
18+
<v-text-field
19+
v-model="password"
20+
:append-icon="
21+
show_password ? 'mdi-visibility' : 'mdi-visibility_off'
22+
"
23+
:type="show_password ? 'text' : 'password'"
24+
:counter="32"
25+
label="password"
26+
prepend-icon="mdi-lock"
27+
@click:append="show_password = !show_password"
28+
/>
29+
</v-form>
30+
</v-card-text>
31+
<v-card-actions>
32+
<v-btn @click="gotoSignin">Login</v-btn>
33+
<v-spacer />
34+
<v-btn @click="doSignin">Signin</v-btn>
35+
</v-card-actions>
36+
<hr />
37+
<v-spacer />
38+
<v-btn @click="gotoResetPassword"
39+
>パスワードを忘れたかたはこちら</v-btn
40+
>
41+
</v-card>
42+
</v-flex>
43+
</v-layout>
44+
</v-container>
45+
</div>
46+
</template>
47+
48+
<script>
49+
import { mapActions } from 'vuex'
50+
import firebase from '@/plugins/firebase'
51+
export default {
52+
data() {
53+
return {
54+
email: '',
55+
password: '',
56+
show_password: false,
57+
}
58+
},
59+
methods: {
60+
...mapActions('modules/user', ['login']),
61+
async doLogin() {
62+
await firebase
63+
.auth()
64+
.createUserWithEmailAndPassword(this.email, this.password)
65+
.then((user) => {
66+
this.login({
67+
uid: user.uid,
68+
email: user.email,
69+
username: user.displayName,
70+
userImage: user.photoURL,
71+
})
72+
this.writeUserData(user.uid, user.email)
73+
this.$router.push('/edit')
74+
})
75+
.catch((error) => {
76+
alert(error)
77+
})
78+
},
79+
gotoSignin() {
80+
this.$router.push('/signin')
81+
},
82+
gotoResetPassword() {
83+
this.$router.push('/reset-password')
84+
},
85+
writeUserData(userId, email) {
86+
return firebase.firestore().collection('classData').doc(userId).set({
87+
email,
88+
})
89+
},
90+
},
91+
}
92+
</script>

src/pages/edit/index.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<template>
2-
<h1>test</h1>
2+
<h1>{{ uid }}</h1>
33
</template>
44

55
<script>
6+
import { mapGetters } from 'vuex'
7+
68
export default {
79
layout: 'protected',
810
name: 'IndexVue',
11+
computed: {
12+
...mapGetters('modules/user', ['uid']),
13+
},
914
}
1015
</script>
1116

src/store/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const actions = {
1111
const classData = classDataSnapshot.data()
1212
await dispatch('modules/class/setClassData', { classData })
1313
if (user) {
14-
await dispatch('modules/user/setUSER', {
14+
await dispatch('modules/user/setUser', {
1515
name: user.name,
1616
email: user.email,
1717
avatar: user.picture,

src/store/modules/class.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
export const state = () => ({
2+
classId: '',
23
classData: {},
34
})
45

56
export const getters = {
67
classData(state) {
78
return state.classData
89
},
10+
classId(state) {
11+
return state.classId
12+
},
913
}
1014

1115
export const mutations = {
1216
setClassData(state, classData) {
1317
state.classData = classData
1418
},
19+
setClassId(state, classId) {
20+
state.classId = classId
21+
},
1522
}
1623

1724
export const actions = {
1825
setClassData({ commit }, classData) {
1926
commit('setClassData', classData)
2027
},
28+
setClassId({ commit }, classId) {
29+
commit('setClassId', classId)
30+
},
2131
}

src/store/modules/user.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import Cookies from 'js-cookie'
2+
import firebase from '@/plugins/firebase.js'
3+
14
export const state = () => ({
25
user: null,
36
info: null,
7+
uid: null,
48
})
59

610
export const mutations = {
@@ -11,6 +15,13 @@ export const mutations = {
1115
state.user = null
1216
}
1317
},
18+
saveUid(state, payload) {
19+
if (payload) {
20+
state.uid = payload
21+
} else {
22+
state.uid = null
23+
}
24+
},
1425
setInfo(state, payload) {
1526
if (payload) {
1627
state.info = payload
@@ -21,9 +32,34 @@ export const mutations = {
2132
}
2233

2334
export const actions = {
35+
async login({ dispatch, state }, user) {
36+
console.log('userlogin')
37+
const token = await firebase.auth().currentUser.getIdToken(true)
38+
const userInfo = {
39+
name: user.displayName,
40+
email: user.email,
41+
avatar: user.photoURL,
42+
uid: user.uid,
43+
}
44+
45+
Cookies.set('access_token', token) // saving token in cookie for server rendering
46+
await dispatch('setUser', userInfo)
47+
await dispatch('saveUid', userInfo.uid)
48+
},
49+
50+
async logout({ commit }) {
51+
await firebase.auth().signOut()
52+
53+
Cookies.remove('access_token')
54+
commit('setUser', null)
55+
commit('saveUid', null)
56+
},
2457
setUser({ commit }, payload) {
2558
commit('setUser', payload)
2659
},
60+
saveUid({ commit }, payload) {
61+
commit('saveUid', payload)
62+
},
2763
setInfo({ commit }, payload) {
2864
commit('setInfo', payload)
2965
},
@@ -36,4 +72,10 @@ export const getters = {
3672
info: (state) => {
3773
return state.info
3874
},
75+
uid: (state) => {
76+
return state.uid
77+
},
78+
isAuthenticated: (state) => {
79+
return !!state.user && !!state.user.uid
80+
},
3981
}

0 commit comments

Comments
 (0)