This repository was archived by the owner on Jul 10, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +57
-22
lines changed Expand file tree Collapse file tree 4 files changed +57
-22
lines changed Original file line number Diff line number Diff line change 1- export default async function ( { store, redirect } : any ) : Promise < void > {
1+ /**
2+ * すでにログインしていたらトップにリダイレクトするミドルウェア
3+ * @param store
4+ * @param redirect
5+ */
6+ export default async function ( { store, redirect } ) : Promise < void > {
27 console . log ( 'anonymous' )
38 if ( store . getters [ 'auth/isAuthenticated' ] ) {
49 await redirect ( '/example' )
Original file line number Diff line number Diff line change 1- export default async function ( { store, redirect } : any ) : Promise < void > {
1+ /**
2+ * ログインしていなかったらログイン画面にリダイレクトするミドルウェア
3+ * @param store
4+ * @param redirect
5+ */
6+
7+ import { getTokenFromCookie } from '@/utilities/'
8+ import { ILoginCheckPayload , ILoginCheck } from '@/interface/User/ILoginCheck'
9+
10+ export default async function ( { store, redirect } ) : Promise < void > {
211 console . log ( 'authenticated' )
12+
13+ const token = getTokenFromCookie ( )
14+ await store . dispatch ( 'auth/loginCheck' , {
15+ token
16+ } as ILoginCheckPayload )
17+
318 if ( ! store . getters [ 'auth/isAuthenticated' ] ) {
419 await redirect ( '/example/auth/sign-in' )
520 }
Original file line number Diff line number Diff line change 11<template lang="pug">
2- section
3- h1.title
4- | sign-in {{counter}} seconds
2+ .login-form
3+ form( @submit.prevent ='login' )
4+ p.error ( v-if ='error' ) {{ error }}
5+ p
6+ input( type ='text' v-model ='username' placeholder ='username' name ='username' )
7+ p
8+ input( type ='text' v-model ='password' placeholder ='password' name ='password' )
9+ .login-btn
10+ button( type ='submit' ) ログイン
511</template >
612
713<script lang="ts">
814import { Component , Vue } from ' nuxt-property-decorator'
9- import { sleep } from ' @/utilities/ '
15+ import { ILoginPayload , IUser } from ' @/interface/User/ILogin '
1016
1117@Component ({
1218 middleware: ' anonymous'
1319})
1420export default class SignIn extends Vue {
15- public counter: number = 3
16-
17- public async mounted() {
18- await sleep (1000 )
19- this .counter = 2
20- await sleep (1000 )
21- this .counter = 1
22- await sleep (1000 )
23- this .counter = 0
24-
25- await this .$router .replace (' /example/auth/signed-in?user=hisasann' )
26- }
21+ public username: string = ' '
22+ public password: string = ' '
23+ public error: string | null = null
2724
2825 public head() {
2926 return {
3027 title: ' sign-in'
3128 }
3229 }
30+
31+ public async login() {
32+ try {
33+ const res: IUser = await this .$store .dispatch (' auth/login' , {
34+ username: this .username ,
35+ password: this .password
36+ } as ILoginPayload )
37+
38+ this .$router .push (' /example' )
39+ } catch (e ) {
40+ this .error = e .message
41+ }
42+ }
3343}
3444 </script >
Original file line number Diff line number Diff line change 55
66<script lang="ts">
77import { Component , Vue } from ' nuxt-property-decorator'
8- import { unsetToken } from ' @/utilities/auth'
8+ import { getTokenFromCookie } from ' @/utilities/'
9+ import { ILogoutPayload } from ' @/interface/User/ILogout'
910
10- @Component
11+ @Component ({
12+ middleware: ' authenticated'
13+ })
1114export default class SignOff extends Vue {
1215 public async mounted() {
13- unsetToken ()
14- await this .$store .dispatch (' auth/logout' )
16+ await this .$store .dispatch (' auth/logout' , {
17+ token: getTokenFromCookie ()
18+ } as ILogoutPayload )
19+
1520 this .$router .replace (' /example' )
1621 }
1722
You can’t perform that action at this time.
0 commit comments