Skip to content
This repository was archived by the owner on Mar 1, 2020. It is now read-only.

Commit 9bf5426

Browse files
committed
add auth-flow example
1 parent 19a56fe commit 9bf5426

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

Diff for: examples/auth-flow/app.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Vue from 'vue/dist/vue'
2+
import VueRouter from 'vue-router'
3+
4+
Vue.use(VueRouter)
5+
6+
import auth from './auth'
7+
import App from './components/App.vue'
8+
import About from './components/About.vue'
9+
import Dashboard from './components/Dashboard.vue'
10+
import Login from './components/Login.vue'
11+
12+
function requireAuth (route, redirect, next) {
13+
if (!auth.loggedIn()) {
14+
redirect({
15+
path: '/login',
16+
query: { redirect: route.fullPath }
17+
})
18+
} else {
19+
next()
20+
}
21+
}
22+
23+
const router = new VueRouter({
24+
mode: 'history',
25+
base: __dirname,
26+
routes: [
27+
{ path: '/about', component: About },
28+
{ path: '/dashboard', component: Dashboard, beforeEnter: requireAuth },
29+
{ path: '/login', component: Login },
30+
{ path: '/logout',
31+
beforeEnter (route, redirect) {
32+
auth.logout()
33+
redirect('/')
34+
}
35+
}
36+
]
37+
})
38+
39+
new Vue(Object.assign({ router }, App)).$mount('#app')

Diff for: examples/auth-flow/auth.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* globals localStorage */
2+
3+
export default {
4+
login (email, pass, cb) {
5+
cb = arguments[arguments.length - 1]
6+
if (localStorage.token) {
7+
if (cb) cb(true)
8+
this.onChange(true)
9+
return
10+
}
11+
pretendRequest(email, pass, (res) => {
12+
if (res.authenticated) {
13+
localStorage.token = res.token
14+
if (cb) cb(true)
15+
this.onChange(true)
16+
} else {
17+
if (cb) cb(false)
18+
this.onChange(false)
19+
}
20+
})
21+
},
22+
23+
getToken () {
24+
return localStorage.token
25+
},
26+
27+
logout (cb) {
28+
delete localStorage.token
29+
if (cb) cb()
30+
this.onChange(false)
31+
},
32+
33+
loggedIn () {
34+
return !!localStorage.token
35+
},
36+
37+
onChange () {}
38+
}
39+
40+
function pretendRequest (email, pass, cb) {
41+
setTimeout(() => {
42+
if (email === '[email protected]' && pass === 'password1') {
43+
cb({
44+
authenticated: true,
45+
token: Math.random().toString(36).substring(7)
46+
})
47+
} else {
48+
cb({ authenticated: false })
49+
}
50+
}, 0)
51+
}

Diff for: examples/auth-flow/components/About.vue

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<h2>About</h2>
4+
</div>
5+
</template>

Diff for: examples/auth-flow/components/App.vue

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<template>
2+
<div id="app">
3+
<h1>Auth Flow</h1>
4+
<ul>
5+
<li>
6+
<router-link v-if="loggedIn" to="/logout">Log out</router-link>
7+
<router-link v-if="!loggedIn" to="/login">Log in</router-link>
8+
</li>
9+
<li>
10+
<router-link to="/about">About</router-link>
11+
</li>
12+
<li>
13+
<router-link to="/dashboard">Dashboard</router-link>
14+
(authenticated)
15+
</li>
16+
</ul>
17+
<template v-if="$route.matched.length">
18+
<router-view></router-view>
19+
</template>
20+
<template v-else>
21+
<p>You are logged {{ loggedIn ? 'in' : 'out' }}</p>
22+
</template>
23+
</div>
24+
</template>
25+
26+
<script>
27+
import auth from '../auth'
28+
29+
export default {
30+
data () {
31+
return {
32+
loggedIn: auth.loggedIn()
33+
}
34+
},
35+
created () {
36+
auth.onChange = loggedIn => {
37+
this.loggedIn = loggedIn
38+
}
39+
}
40+
}
41+
</script>

Diff for: examples/auth-flow/components/Dashboard.vue

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<h2>Dashboard</h2>
4+
<p>Yay you made it!</p>
5+
</div>
6+
</template>

Diff for: examples/auth-flow/components/Login.vue

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<template>
2+
<div>
3+
<h2>Login</h2>
4+
<p v-if="$route.query.redirect">
5+
You need to login first.
6+
</p>
7+
<form @submit.prevent="login">
8+
<label><input v-model="email" placeholder="email"></label>
9+
<label><input v-model="pass" placeholder="password" type="password"></label> (hint: password1)<br>
10+
<button type="submit">login</button>
11+
<p v-if="error" style="color:red">Bad login information</p>
12+
</form>
13+
</div>
14+
</template>
15+
16+
<script>
17+
import auth from '../auth'
18+
19+
export default {
20+
data () {
21+
return {
22+
23+
pass: '',
24+
error: false
25+
}
26+
},
27+
methods: {
28+
login () {
29+
auth.login(this.email, this.pass, loggedIn => {
30+
if (!loggedIn) {
31+
this.error = true
32+
} else {
33+
this.$router.replace(this.$route.query.redirect || '/')
34+
}
35+
})
36+
}
37+
}
38+
}
39+
</script>

Diff for: examples/auth-flow/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<a href="/">Examples index</a>
3+
<div id="app"></div>
4+
<script src="/__build__/shared.js"></script>
5+
<script src="/__build__/auth-flow.js"></script>

0 commit comments

Comments
 (0)