├── .editorconfig ├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── assets ├── 1.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png └── 8.png ├── components ├── Logo.vue └── error │ ├── 404.vue │ └── 500.vue ├── jsconfig.json ├── layouts ├── default.vue └── error.vue ├── middleware ├── authenticated.js ├── notAuthenticated.js └── refreshToken.js ├── nuxt.config.js ├── package.json ├── pages ├── auth │ ├── confirm │ │ └── _email │ │ │ └── index.vue │ ├── login.vue │ └── register.vue ├── index.vue └── secret.vue ├── plugins └── setTokenAxios.js ├── server └── index.js ├── static ├── favicon.ico └── icon.png └── store └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Elastic Beanstalk Files 2 | .elasticbeanstalk/* 3 | !.elasticbeanstalk/*.cfg.yml 4 | !.elasticbeanstalk/*.global.yml 5 | # Created by .ignore support plugin (hsz.mobi) 6 | ### Node template 7 | # Logs 8 | logs 9 | *.log 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | 14 | # Runtime data 15 | pids 16 | *.pid 17 | *.seed 18 | *.pid.lock 19 | 20 | # Directory for instrumented libs generated by jscoverage/JSCover 21 | lib-cov 22 | 23 | # Coverage directory used by tools like istanbul 24 | coverage 25 | 26 | # nyc test coverage 27 | .nyc_output 28 | 29 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 30 | .grunt 31 | 32 | # Bower dependency directory (https://bower.io/) 33 | bower_components 34 | 35 | # node-waf configuration 36 | .lock-wscript 37 | 38 | # Compiled binary addons (https://nodejs.org/api/addons.html) 39 | build/Release 40 | 41 | # Dependency directories 42 | node_modules/ 43 | jspm_packages/ 44 | 45 | # TypeScript v1 declaration files 46 | typings/ 47 | 48 | # Optional npm cache directory 49 | .npm 50 | 51 | # Optional eslint cache 52 | .eslintcache 53 | 54 | # Optional REPL history 55 | .node_repl_history 56 | 57 | # Output of 'npm pack' 58 | *.tgz 59 | 60 | # Yarn Integrity file 61 | .yarn-integrity 62 | 63 | # dotenv environment variables file 64 | .env 65 | 66 | # parcel-bundler cache (https://parceljs.org/) 67 | .cache 68 | 69 | # next.js build output 70 | .next 71 | 72 | # nuxt.js build output 73 | .nuxt 74 | 75 | # Nuxt generate 76 | dist 77 | 78 | # vuepress build output 79 | .vuepress/dist 80 | 81 | # Serverless directories 82 | .serverless 83 | 84 | # IDE / Editor 85 | .idea 86 | 87 | # Service worker 88 | sw.* 89 | 90 | # Mac OSX 91 | .DS_Store 92 | 93 | # Vim swap files 94 | *.swp 95 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": false, 3 | "singleQuote": true 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Klaudiusz Marszałek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nuxt.js & AWS Cognito JWT authentication 2 | This is Nuxt.js SSR boilerplate app with express.js sever and httpOnly cookie based authentication. You can find out how to securely add sign-up and sign-in functionality to your app and how to get, store and refresh JWT token. 3 | 4 | ## Instalation steps 5 | ### 1. Create AWS account and go to the Cognito settings 6 | ### 2. Click Manage User Pools 7 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/1.png) 8 | ### 3. Type pool name and click Step through settings 9 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/2.png) 10 | ### 4. In the attributes tab check the options as below and click next 11 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/3.png) 12 | ### 5. In the policies tab select as below 13 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/4.png) 14 | ### 6. Next in the MFA tab select as below 15 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/5.png) 16 | ### 7. Then in the App clients tab add an app client 17 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/6.png) 18 | ### 8. Afterwards check as below 19 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/7.png) 20 | ### 9. Next two steps you can skip and finally you will see some thing like this 21 | ![](https://github.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/blob/master/assets/8.png) 22 | ### 10. Create .env file and save your Pool Id, App client id and Pool region 23 | #### .env file in the root folder 24 | 25 | UserPoolId=eu-central-1_X6gk9q9wU 26 | ClientId=34awgjes6resft2q6w5dwry64 27 | PoolRegion=eu-central-1 28 | 29 | ### 11. Finally install and run your app 30 | `npm install` 31 | 32 | `npm run dev` 33 | -------------------------------------------------------------------------------- /assets/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/1.png -------------------------------------------------------------------------------- /assets/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/2.png -------------------------------------------------------------------------------- /assets/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/3.png -------------------------------------------------------------------------------- /assets/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/4.png -------------------------------------------------------------------------------- /assets/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/5.png -------------------------------------------------------------------------------- /assets/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/6.png -------------------------------------------------------------------------------- /assets/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/7.png -------------------------------------------------------------------------------- /assets/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/assets/8.png -------------------------------------------------------------------------------- /components/Logo.vue: -------------------------------------------------------------------------------- 1 | 9 | 10 | 80 | -------------------------------------------------------------------------------- /components/error/404.vue: -------------------------------------------------------------------------------- 1 | 10 | 21 | -------------------------------------------------------------------------------- /components/error/500.vue: -------------------------------------------------------------------------------- 1 | 10 | -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": ".", 4 | "paths": { 5 | "~/*": ["./*"], 6 | "@/*": ["./*"], 7 | "~~/*": ["./*"], 8 | "@@/*": ["./*"] 9 | } 10 | }, 11 | "exclude": ["node_modules", ".nuxt", "dist"] 12 | } 13 | -------------------------------------------------------------------------------- /layouts/default.vue: -------------------------------------------------------------------------------- 1 | 20 | 43 | 92 | -------------------------------------------------------------------------------- /layouts/error.vue: -------------------------------------------------------------------------------- 1 | 6 | -------------------------------------------------------------------------------- /middleware/authenticated.js: -------------------------------------------------------------------------------- 1 | export default function ({ store, redirect }) { 2 | if(store.state.auth) { 3 | if(new Date(store.state.auth.te) < new Date(Date.now())){ 4 | store.dispatch("clearAuth", redirect); 5 | } 6 | } else { 7 | return redirect('/login') 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /middleware/notAuthenticated.js: -------------------------------------------------------------------------------- 1 | export default function ({ store, redirect }) { 2 | if (store.state.auth) { 3 | return redirect('/') 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /middleware/refreshToken.js: -------------------------------------------------------------------------------- 1 | export default function ({ store }) { 2 | if(process.client) { 3 | store.dispatch('refreshToken') 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /nuxt.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | mode: 'universal', 3 | /* 4 | ** Headers of the page 5 | */ 6 | head: { 7 | title: process.env.npm_package_name || '', 8 | meta: [ 9 | { charset: 'utf-8' }, 10 | { name: 'viewport', content: 'width=device-width, initial-scale=1' }, 11 | { hid: 'description', name: 'description', content: process.env.npm_package_description || '' } 12 | ], 13 | link: [ 14 | { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' } 15 | ] 16 | }, 17 | /* 18 | ** Customize the progress-bar color 19 | */ 20 | loading: { color: '#fff' }, 21 | /* 22 | ** Global CSS 23 | */ 24 | css: [ 25 | ], 26 | /* 27 | ** Plugins to load before mounting the App 28 | */ 29 | plugins: [ 30 | '~/plugins/setTokenAxios' 31 | ], 32 | /* 33 | ** Nuxt.js dev-modules 34 | */ 35 | buildModules: [ 36 | ], 37 | /* 38 | ** Nuxt.js modules 39 | */ 40 | modules: [ 41 | // Doc: https://bootstrap-vue.js.org 42 | 'bootstrap-vue/nuxt', 43 | // Doc: https://axios.nuxtjs.org/usage 44 | '@nuxtjs/axios', 45 | '@nuxtjs/pwa', 46 | ], 47 | /* 48 | ** Axios module configuration 49 | ** See https://axios.nuxtjs.org/options 50 | */ 51 | axios: { 52 | baseURL: process.env.BASE_URL || 'http://localhost:3000' 53 | }, 54 | env: { 55 | baseUrl: process.env.BASE_URL || 'http://localhost:3000', 56 | }, 57 | /* 58 | ** Build configuration 59 | */ 60 | build: { 61 | /* 62 | ** You can extend webpack config here 63 | */ 64 | extend (config, ctx) { 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nuxt-ssr-aws-cognito", 3 | "version": "1.0.0", 4 | "description": "nuxt ssr aws cognito", 5 | "author": "Klaudiusz Marszalek", 6 | "private": true, 7 | "scripts": { 8 | "dev": "cross-env NODE_ENV=development nodemon server/index.js --watch server", 9 | "build": "nuxt build", 10 | "start": "cross-env NODE_ENV=production node server/index.js", 11 | "generate": "nuxt generate" 12 | }, 13 | "dependencies": { 14 | "@nuxtjs/axios": "^5.8.0", 15 | "@nuxtjs/pwa": "^3.0.0-0", 16 | "amazon-cognito-identity-js": "^3.2.0", 17 | "aws-sdk": "^2.575.0", 18 | "body-parser": "^1.19.0", 19 | "bootstrap": "^4.3.1", 20 | "bootstrap-vue": "^2.1.0", 21 | "cookie-parser": "^1.4.4", 22 | "cookieparser": "^0.1.0", 23 | "cross-env": "^6.0.3", 24 | "dotenv": "^8.2.0", 25 | "express": "^4.17.1", 26 | "js-cookie": "^2.2.1", 27 | "jsonwebtoken": "^8.5.1", 28 | "jwk-to-pem": "^2.0.1", 29 | "node-fetch": "^2.6.0", 30 | "nuxt": "^2.10.2" 31 | }, 32 | "devDependencies": { 33 | "nodemon": "^1.19.4", 34 | "eslint-config-prettier": "^6.6.0", 35 | "eslint-plugin-prettier": "^3.1.1", 36 | "prettier": "^1.19.1" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pages/auth/confirm/_email/index.vue: -------------------------------------------------------------------------------- 1 | 22 | 23 | -------------------------------------------------------------------------------- /pages/auth/login.vue: -------------------------------------------------------------------------------- 1 | 27 | 28 | -------------------------------------------------------------------------------- /pages/auth/register.vue: -------------------------------------------------------------------------------- 1 | 31 | 32 | -------------------------------------------------------------------------------- /pages/index.vue: -------------------------------------------------------------------------------- 1 | 25 | 26 | 78 | 79 | 118 | -------------------------------------------------------------------------------- /pages/secret.vue: -------------------------------------------------------------------------------- 1 | 11 | 12 | 38 | -------------------------------------------------------------------------------- /plugins/setTokenAxios.js: -------------------------------------------------------------------------------- 1 | export default ({ $axios, store }) => { 2 | // $axios.defaults.baseURL = 'http://localhost:3000' 3 | 4 | // if (process.server) { 5 | // return 6 | // } 7 | 8 | // $axios.interceptors.request.use(request => { 9 | // request.baseURL = 'http://localhost:3000' 10 | 11 | // // Get token from auth.js store 12 | // const token = store.state.token 13 | 14 | // // Update token axios header 15 | // if (token) { 16 | // request.headers.common['Authorization'] = token 17 | // } 18 | // return request 19 | // }) 20 | return 21 | } -------------------------------------------------------------------------------- /server/index.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const consola = require('consola') 3 | const { Nuxt, Builder } = require('nuxt') 4 | const app = express() 5 | const cookiepars = require('cookieparser') 6 | const bodyParser = require('body-parser') 7 | const cookieParser = require('cookie-parser') 8 | const AmazonCognitoIdentity = require('amazon-cognito-identity-js'); 9 | 10 | global.fetch = require('node-fetch'); 11 | require('dotenv').config(); 12 | 13 | const config = require('../nuxt.config.js') 14 | config.dev = process.env.NODE_ENV !== 'production' 15 | 16 | async function start () { 17 | 18 | const nuxt = new Nuxt(config) 19 | const { host, port } = nuxt.options.server 20 | 21 | const userPool = new AmazonCognitoIdentity.CognitoUserPool({ 22 | UserPoolId : process.env.UserPoolId, // Pool Id 23 | ClientId : process.env.ClientId, // App client id 24 | }); 25 | 26 | if (config.dev) { 27 | const builder = new Builder(nuxt) 28 | await builder.build() 29 | } else { 30 | await nuxt.ready() 31 | } 32 | 33 | app.use(cookieParser()); 34 | app.use(bodyParser.urlencoded({ extended: false })) 35 | app.use(bodyParser.json()) 36 | 37 | app.post('/auth/register', (req, res, next) => { 38 | 39 | const attributeList = [ 40 | new AmazonCognitoIdentity.CognitoUserAttribute({Name: "email", Value: req.body.email}), 41 | new AmazonCognitoIdentity.CognitoUserAttribute({Name: "phone_number", Value: req.body.phone}), 42 | new AmazonCognitoIdentity.CognitoUserAttribute({Name: "custom:role", Value: "dealer"}) 43 | ] 44 | 45 | userPool.signUp(req.body.email, req.body.password, attributeList, null, function(err, result){ 46 | if (err) { 47 | try { 48 | err.message = err.message; 49 | return res.status(400).json({error: err}); 50 | } catch (err) { 51 | console.log(err); 52 | return res.status(500) 53 | } 54 | } 55 | cognitoUser = result.user; 56 | res.json({user: cognitoUser.getUsername()}); 57 | }); 58 | }) 59 | 60 | app.post('/auth/login', (req, res, next) => { 61 | const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({ 62 | Username : req.body.email, 63 | Password : req.body.password, 64 | }); 65 | 66 | const cognitoUser = new AmazonCognitoIdentity.CognitoUser({ 67 | Username : req.body.email, 68 | Pool : userPool 69 | }); 70 | 71 | cognitoUser.authenticateUser(authenticationDetails, { 72 | onSuccess: function(result) { 73 | const expirationTime = new Date(result.getAccessToken().payload.exp*1000) 74 | const auth = { 75 | jwt: result.getAccessToken().getJwtToken(), 76 | jwt_expired: expirationTime, 77 | email: req.body.email 78 | } 79 | res.cookie('token', result.getRefreshToken().getToken(), { 80 | expires: new Date(result.getAccessToken().payload.auth_time * 1000 + 1000*60*60*24*30), 81 | httpOnly: true }).json(auth); 82 | }, 83 | onFailure: function(err) { 84 | if (err) { 85 | try { 86 | err.message = err.message; 87 | return res.status(400).json({error: err}); 88 | } catch (err) { 89 | console.log(err); 90 | return res.status(500) 91 | } 92 | } 93 | }, 94 | }); 95 | }) 96 | 97 | app.post('/auth/logout', (req, res) => { 98 | res.cookie('token','', { 99 | expires: new Date(Date.now()), 100 | httpOnly: true }).json({ ok: true }); 101 | }) 102 | 103 | app.post('/auth/refresh-token', (req, res) => { 104 | if (req.headers.cookie) { 105 | const parsed = cookiepars.parse(req.headers.cookie) 106 | const refreshToken = new AmazonCognitoIdentity.CognitoRefreshToken({RefreshToken: parsed.token}); 107 | const cognitoUser = new AmazonCognitoIdentity.CognitoUser({ 108 | Username : '', 109 | Pool : userPool 110 | }); 111 | 112 | cognitoUser.refreshSession(refreshToken, (err, result) => { 113 | if (err) { 114 | res.status(401).json({ message: 'The Access Token expired' }) 115 | } else { 116 | const expirationTime = new Date(result.getAccessToken().payload.exp*1000) 117 | const auth = { 118 | jwt: result.getIdToken().getJwtToken(), 119 | jwt_expired: expirationTime, 120 | email: result.getIdToken().payload.email, 121 | refresh_token: result.getRefreshToken().getToken(), 122 | refresh_token_expired: new Date(result.getIdToken().payload.auth_time * 1000 + 1000*60*60*24*30) 123 | } 124 | res.cookie('token', result.getRefreshToken().getToken(), { 125 | expires: new Date(result.getAccessToken().payload.auth_time * 1000 + 1000*60*60*24*30), 126 | httpOnly: true }).json(auth); 127 | } 128 | }) 129 | } else { 130 | res.status(401).json({ message: 'Missing auth cookie' }) 131 | } 132 | }) 133 | 134 | app.post('/auth/confirm', (req, res, next) => { 135 | 136 | const cognitoUser = new AmazonCognitoIdentity.CognitoUser({ 137 | Username : req.body.email, 138 | Pool : userPool 139 | }); 140 | 141 | cognitoUser.confirmRegistration(req.body.code, true, function(err, result) { 142 | if (err) { 143 | console.log(err); 144 | err.message = err.message; 145 | return res.status(400).json({error: err.message}); 146 | } 147 | res.json({success: true}); 148 | }) 149 | }) 150 | 151 | app.use(nuxt.render) 152 | 153 | app.listen(port, host) 154 | 155 | consola.ready({ 156 | message: `Server listening on http://${host}:${port}`, 157 | badge: true 158 | }) 159 | } 160 | start() 161 | -------------------------------------------------------------------------------- /static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/static/favicon.ico -------------------------------------------------------------------------------- /static/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ja-klaudiusz/Nuxt.js-SSR-AWS-Cognito/f1830407da44663bb1c5f692d7b046a9b3d394fe/static/icon.png -------------------------------------------------------------------------------- /store/index.js: -------------------------------------------------------------------------------- 1 | const cookieparser = process.server ? require('cookieparser') : undefined 2 | 3 | export const state = () => { 4 | return { 5 | auth: null 6 | } 7 | } 8 | export const mutations = { 9 | setAuth (state, auth, error) { 10 | state.auth = auth 11 | if(error) console.log('[MUTATION] setAuth', error); 12 | }, 13 | clearAuth(state, redirect) { 14 | state.auth = null 15 | redirect('/auth/login') 16 | } 17 | } 18 | export const actions = { 19 | nuxtServerInit ({ commit, dispatch }, { req, res }) { 20 | if (req.headers.cookie) { 21 | const parsed = cookieparser.parse(req.headers.cookie) 22 | try { 23 | if(parsed.token) { 24 | return this.$axios.$post('/auth/refresh-token').then(result => { 25 | try { 26 | commit('setAuth',result) 27 | res.cookie('token', result.refresh_token, { expires: new Date(result.refresh_token_expired), httpOnly: true }) 28 | } catch(err) { 29 | commit('setAuth', null, err) 30 | } 31 | }).catch((err)=>{ 32 | commit('setAuth', null, err) 33 | }); 34 | } else { 35 | commit('setAuth',null) 36 | } 37 | } catch (err) { 38 | commit('setAuth', null, err) 39 | } 40 | } 41 | }, 42 | clearAuth({ commit }, redirect) { 43 | commit('clearAuth', redirect) 44 | }, 45 | refreshToken({commit, state}) { 46 | if(state.auth) { 47 | if(new Date(Date.now()) > new Date(state.auth.jwt_expired)) { 48 | return this.$axios.$post('/auth/refresh-token').then(result => { 49 | try { 50 | commit('setAuth',result) 51 | } catch(err) { 52 | commit('setAuth', null, err) 53 | } 54 | }).catch((err)=>{ 55 | commit('setAuth', null, err) 56 | }); 57 | } 58 | } else { 59 | commit('setAuth', null) 60 | } 61 | } 62 | } 63 | --------------------------------------------------------------------------------