-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework examples and refactor session handling #322
Changes from 1 commit
dcec2f8
d28e575
b43d9be
e095e13
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import { user as getUser } from '@/utils/auth' | ||
|
||
export default function ({ store, req, route, redirect }) { | ||
export default async ({ | ||
redirect, | ||
route, | ||
store, | ||
req, | ||
$axios | ||
}) => { | ||
// If nuxt generate, pass this middleware | ||
if (process.static) return | ||
const user = getUser(process.server ? req : null) | ||
if (user && !store.state.authUser) { | ||
store.commit('SET_USER', user) | ||
} else if (!user && route.name !== 'login') { | ||
const maybeReq = process.server ? req : null | ||
const hasSession = maybeReq !== null && !!maybeReq.session | ||
let maybeAuthenticated = await store.getters.authenticated | ||
if (hasSession === true && maybeAuthenticated === false) { | ||
const { data } = await $axios.get('/hpi/auth/whois') | ||
store.commit('SET_USER', data) | ||
maybeAuthenticated = data.authenticated || false | ||
} | ||
const currentPath = route.path | ||
const isNotLogin = currentPath !== '/login' | ||
if (isNotLogin && maybeAuthenticated === false) { | ||
redirect('/login', { page: route.fullPath }) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,6 @@ | |
|
||
<script> | ||
import Vue from 'vue' | ||
import axios from 'axios' | ||
import debounce from '@/utils/debounce' | ||
import Component from 'class-component' | ||
|
||
|
@@ -56,6 +55,7 @@ export default class Login extends Vue { | |
password: '', | ||
captcha: '' | ||
} | ||
authenticated = false // #WatchHowDoWe ... How do we? | ||
rules = {} | ||
captchaSvg = '' | ||
// keepPwd = false | ||
|
@@ -67,31 +67,57 @@ export default class Login extends Vue { | |
this.getCaptcha() | ||
} | ||
async login () { | ||
const goBackTo = this.$route.query.page || '/' | ||
this.logging = true | ||
this.$refs.user.validate(async (valid) => { | ||
try { | ||
if (valid) { | ||
await this.$store.dispatch('login', this.user) | ||
this.$router.push(this.$route.query.page || '/') | ||
} | ||
} catch (e) { | ||
this.$message.warning(e.message) | ||
} finally { | ||
this.logging = false | ||
const valid = this.$refs.user.validate() | ||
try { | ||
if (valid) { | ||
await this.$store.dispatch('login', this.user) | ||
this.authenticated = await this.$store.getters.authenticated | ||
} | ||
} catch (e) { | ||
this.$message.warning(e.message) | ||
} finally { | ||
if (this.authenticated) { | ||
this.redirect(goBackTo) | ||
} else { | ||
const message = 'Not authenticated' | ||
this.$message.warning(message) | ||
} | ||
}) | ||
} | ||
this.logging = false | ||
} | ||
async getCaptcha () { | ||
redirect (goTo) { | ||
this.$router.push(goTo) | ||
} | ||
getCaptcha () { | ||
const params = {} | ||
if (this.$refs.captcha) { | ||
params.width = this.$refs.captcha.$el.clientWidth || 150 | ||
params.height = this.$refs.captcha.$el.clientHeight || 36 | ||
} | ||
const {data: captcha} = await axios.get('/hpi/auth/captcha', { params }) | ||
this.captchaSvg = captcha | ||
this.captchaSvg = this.$axios.get('/hpi/auth/captcha', { params }) | ||
.then(response => { | ||
// #WatchHowDoWe | ||
// Just passing through :| | ||
// TODO, improve this, figure out how @watch works | ||
const authenticated = this.$store.getters.authenticated | ||
if (authenticated) { | ||
this.redirect('/') | ||
} | ||
const data = response.data | ||
return data | ||
}) | ||
.then(captcha => { | ||
this.captchaSvg = captcha | ||
}) | ||
.catch(error => { | ||
const errorMessage = error.toString() | ||
this.captchaSvg = `<small style="line-height:1em;display:block;">${errorMessage}</small>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was no error management, now that makes it be more elegant if it breaks. |
||
}) | ||
} | ||
|
||
refreshCaptcha = debounce(this.getCaptcha, 500) | ||
refreshCaptcha = debounce(this.getCaptcha, 500) // Note to you, reader, does this still work? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this really work? |
||
} | ||
</script> | ||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export const state = () => ({ | ||
activities: [ | ||
{ | ||
'account': '0', | ||
'date': '2018-01-01', | ||
'type': 'price', | ||
'region': '北京', | ||
'priority': '高', | ||
'organizer': '市场部', | ||
'desc': 'Activity 0, as a default Vuex activity entry' | ||
} | ||
] | ||
}) | ||
|
||
export const mutations = { | ||
SET_ACTIVITIES ( | ||
state, | ||
values | ||
) { | ||
for (const activity of values) { | ||
state.activities.push(Object.assign({}, activity)) | ||
} | ||
} | ||
} | ||
|
||
export const actions = { | ||
add ({ commit }, activity) { | ||
const payload = [activity] | ||
commit('SET_ACTIVITIES', payload) | ||
} | ||
} | ||
|
||
export const getters = { | ||
activities (state) { | ||
return state.activities | ||
}, | ||
title (state) { | ||
return 'activity.title.create' | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #321 ; Delegate to Koa all data management.