mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
Add JWT example source code (#3746)
* Add JWT exmaple source code * add sample code * [fix] eslint * Update index.js * [fix] rename middleware\ / to middleware/ * Add default value for demo
This commit is contained in:
parent
f9bdac2452
commit
7161c6f191
3
examples/auth-jwt/README.md
Normal file
3
examples/auth-jwt/README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
# Auth External API (JWT) with Nuxt.js
|
||||||
|
|
||||||
|
https://nuxtjs.org/examples/auth-external-jwt
|
6
examples/auth-jwt/middleware/authenticated.js
Normal file
6
examples/auth-jwt/middleware/authenticated.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default function ({ store, redirect }) {
|
||||||
|
// If the user is not authenticated
|
||||||
|
if (!store.state.auth) {
|
||||||
|
return redirect('/login')
|
||||||
|
}
|
||||||
|
}
|
6
examples/auth-jwt/middleware/notAuthenticated.js
Normal file
6
examples/auth-jwt/middleware/notAuthenticated.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export default function ({ store, redirect }) {
|
||||||
|
// If the user is authenticated redirect to home page
|
||||||
|
if (store.state.auth) {
|
||||||
|
return redirect('/')
|
||||||
|
}
|
||||||
|
}
|
13
examples/auth-jwt/package.json
Normal file
13
examples/auth-jwt/package.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "example-auth-jwt",
|
||||||
|
"dependencies": {
|
||||||
|
"cookieparser": "^0.1.0",
|
||||||
|
"js-cookie": "^2.2.0",
|
||||||
|
"nuxt": "latest"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"dev": "nuxt",
|
||||||
|
"build": "nuxt build",
|
||||||
|
"start": "nuxt start"
|
||||||
|
}
|
||||||
|
}
|
34
examples/auth-jwt/pages/login.vue
Normal file
34
examples/auth-jwt/pages/login.vue
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<template>
|
||||||
|
<div class="container">
|
||||||
|
<h1>login view</h1>
|
||||||
|
<div>
|
||||||
|
<label for="email">
|
||||||
|
<input id="email" type="email" value="test">
|
||||||
|
</label>
|
||||||
|
<label for="password">
|
||||||
|
<input id="password" type="password" value="test">
|
||||||
|
</label>
|
||||||
|
<button @click="postLogin">login</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
import Cookie from 'js-cookie'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
middleware: 'notAuthenticated',
|
||||||
|
methods: {
|
||||||
|
postLogin() {
|
||||||
|
setTimeout(() => { // we simulate the async request with timeout.
|
||||||
|
const auth = {
|
||||||
|
accessToken: 'someStringGotFromApiServiceWithAjax'
|
||||||
|
}
|
||||||
|
this.$store.commit('update', auth) // mutating to store for client rendering
|
||||||
|
Cookie.set('auth', auth) // saving token in cookie for server rendering
|
||||||
|
this.$router.push('/')
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
9
examples/auth-jwt/pages/secret.vue
Normal file
9
examples/auth-jwt/pages/secret.vue
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<template>
|
||||||
|
<h1>this is secret page.</h1>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
middleware: 'authenticated'
|
||||||
|
}
|
||||||
|
</script>
|
28
examples/auth-jwt/store/index.js
Normal file
28
examples/auth-jwt/store/index.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import Vuex from 'vuex'
|
||||||
|
|
||||||
|
const cookieparser = process.server ? require('cookieparser') : undefined
|
||||||
|
|
||||||
|
const createStore = () => {
|
||||||
|
return new Vuex.Store({
|
||||||
|
state: {
|
||||||
|
auth: null
|
||||||
|
},
|
||||||
|
mutations: {
|
||||||
|
update(state, data) {
|
||||||
|
state.auth = data
|
||||||
|
}
|
||||||
|
},
|
||||||
|
actions: {
|
||||||
|
nuxtServerInit({ commit }, { req }) {
|
||||||
|
let accessToken = null
|
||||||
|
if (req.headers.cookie) {
|
||||||
|
const parsed = cookieparser.parse(req.headers.cookie)
|
||||||
|
accessToken = JSON.parse(parsed.auth)
|
||||||
|
}
|
||||||
|
commit('update', accessToken)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export default createStore
|
Loading…
Reference in New Issue
Block a user