Skip to content

Commit 71901a8

Browse files
committed
ADD: Auth
1 parent 1d22315 commit 71901a8

File tree

12 files changed

+154
-237
lines changed

12 files changed

+154
-237
lines changed

nuxt.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ module.exports = {
88
** Headers of the page
99
*/
1010
head: {
11+
htmlAttrs: {
12+
prefix: 'og: http://ogp.me/ns#',
13+
},
1114
title: 'おうちで時間割',
1215
meta: [
1316
{ charset: 'utf-8' },
@@ -23,6 +26,7 @@ module.exports = {
2326
link: [
2427
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
2528
{ rel: 'apple-touch-icon', href: '/apple-touch-icon-precomposed.png' },
29+
{ hid: 'og:type', property: 'og:type', content: 'website' },
2630
{
2731
hid: 'og:image',
2832
property: 'og:image',
@@ -55,6 +59,10 @@ module.exports = {
5559
{
5660
src: '@/plugins/firebase',
5761
},
62+
{
63+
src: '@/plugins/persistedstate.js',
64+
ssr: false,
65+
},
5866
],
5967
/*
6068
** Nuxt.js dev-modules

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
"nuxt": "^2.12.2",
4242
"nuxt-svg-loader": "^1.2.0",
4343
"nuxt-webfontloader": "^1.1.0",
44-
"uuid": "^8.0.0"
44+
"uuid": "^8.0.0",
45+
"vuex-persistedstate": "^3.0.1"
4546
},
4647
"devDependencies": {
4748
"@nuxtjs/eslint-config": "^2.0.2",

src/middleware/authenticated.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
export default function ({ store, redirect }) {
2-
if (!store.getters['modules/user/isAuthenticated']) {
3-
return redirect('/auth/signin')
4-
}
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+
})
516
}

src/middleware/handle-login-route.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/pages/account/login.vue

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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="gotoSignup">SIGNUP</v-btn>
33+
<v-spacer />
34+
<v-btn @click="doLogin">LOGIN</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 firebase from '@/plugins/firebase'
50+
export default {
51+
data() {
52+
return {
53+
email: '',
54+
password: '',
55+
show_password: false,
56+
}
57+
},
58+
methods: {
59+
doLogin() {
60+
firebase
61+
.auth()
62+
.signInWithEmailAndPassword(this.email, this.password)
63+
.then((user) => {
64+
this.$router.push('/edit')
65+
})
66+
.catch((error) => {
67+
alert(error)
68+
})
69+
},
70+
gotoSignup() {
71+
this.$router.push('/signup')
72+
},
73+
gotoResetPassword() {
74+
this.$router.push('/reset-password')
75+
},
76+
},
77+
}
78+
</script>

src/pages/auth/signin.vue

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/pages/auth/signup.vue

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/plugins/firebase.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,4 @@ if (!firebase.apps.length) {
1616
firebase.initializeApp(firebaseConfig)
1717
}
1818

19-
export default (context, inject) => {
20-
inject('firebase', firebase)
21-
inject('firestore', firebase.firestore())
22-
inject('auth', firebase.auth())
23-
}
19+
export default firebase

src/plugins/persistedstate.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import createPersistedState from 'vuex-persistedstate'
2+
3+
export default ({ store, isHMR }) => {
4+
if (isHMR) return
5+
6+
if (process.client) {
7+
window.onNuxtReady(() => {
8+
createPersistedState({})(store) // vuex plugins can be connected to store, even after creation
9+
})
10+
}
11+
}

src/store/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { getUserFromCookie } from '@/helpers'
2-
2+
import firebase from '@/plugins/firebase'
33
export const actions = {
44
async nuxtServerInit({ dispatch, commit, state }, { req, res, error }) {
55
const user = getUserFromCookie(req)
6-
const classDataSnapshot = await this.$firestore
6+
const classDataSnapshot = await firebase
7+
.firestore()
78
.collection('classData')
89
.doc('あけしめたす')
910
.get()

0 commit comments

Comments
 (0)