Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
Change store's structure and bring login dialog back
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualprism committed Jan 13, 2019
1 parent 492cf9c commit 5c938d0
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 123 deletions.
62 changes: 35 additions & 27 deletions components/public/header/user.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
</template>

<script>
import { mapState } from 'vuex'
import LoginDialog from '@/components/public/login-dialog.vue'
import { mapState, mapMutations } from 'vuex'
export default {
data() {
return {
Expand All @@ -58,34 +59,41 @@ export default {
},
methods: {
login() {
/* global FB */
FB.login(
async response => {
try {
await this.$axios.post('/login', {
authResponse: response.authResponse
})
this.$store.dispatch('updateUserState')
this.$router.replace('/')
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
if (this.envMode === 'production') {
/* global FB */
FB.login(
async response => {
try {
await this.$axios.post('/login', {
authResponse: response.authResponse
})
this.$store.dispatch('updateUserState')
this.$nuxt.error({ statusCode: 401 })
} else if (
code === 403 &&
e.response.data.reason === 'unregistered'
) {
this.$store.dispatch('updateUserState')
this.$router.push('/signup')
} else {
throw e
this.$router.replace('/')
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
this.$store.dispatch('updateUserState')
this.$nuxt.error({ statusCode: 401 })
} else if (
code === 403 &&
e.response.data.reason === 'unregistered'
) {
this.$store.dispatch('updateUserState')
this.$router.push('/signup')
} else {
throw e
}
}
}
},
{ scope: 'public_profile,email' }
)
}
},
{ scope: 'public_profile,email' }
)
} else {
this.showLoginDialog()
}
},
...mapMutations({
showLoginDialog: 'showLoginDialog'
})
}
}
</script>
63 changes: 63 additions & 0 deletions components/public/login-dialog.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<template>
<el-dialog
:visible="dialogVisible"
width="35%"
title="Login for development"
@close="closeDialog">
<el-form>
<el-form-item label="Facebook user_id">
<el-input
v-model="userId"
autocomplete="off"/>
</el-form-item>
</el-form>
<span
slot="footer"
class="dialog-footer">
<el-button @click="closeDialog">Cancel</el-button>
<el-button
type="primary"
@click="login">
Confirm
</el-button>
</span>
</el-dialog>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
data() {
return {
userId: ''
}
},
computed: {
dialogVisible() {
return this.$store.state.dialogLoginVisible
}
},
methods: {
async login() {
try {
await this.$axios.post('/dev/login', { user_id: this.userId })
this.$store.dispatch('updateUserState')
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 403 && e.response.data.reason === 'unregistered') {
this.$store.dispatch('updateUserState')
this.$router.push('/signup')
} else {
throw e
}
} finally {
this.closeDialog()
}
},
closeDialog() {
this.$store.commit('hideLoginDialog')
}
},
}
</script>
5 changes: 4 additions & 1 deletion layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<my-header />
<v-content>
<nuxt />
<login-dialog/>
</v-content>
<my-footer />
</v-app>
Expand All @@ -11,10 +12,12 @@
<script>
import MyHeader from '../components/public/header/index.vue'
import MyFooter from '../components/public/footer/index.vue'
import LoginDialog from '@/components/public/login-dialog.vue'
export default {
components: {
MyHeader,
MyFooter
MyFooter,
LoginDialog
}
}
</script>
Expand Down
62 changes: 34 additions & 28 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
<h2 class="blue--text font-weight-bold">推薦商品</h2>
<good-list class="good-list" />
</el-card>

</div>
</template>

Expand All @@ -88,7 +87,7 @@ import Emenu from '@/components/index/menu.vue'
import Banner from '@/components/index/banner.vue'
import GoodList from '@/components/index/good-list.vue'
import HomeHeader from '@/components/index/home-header.vue'
import { mapState } from 'vuex'
import { mapState, mapMutations } from 'vuex'
export default {
components: {
Emenu,
Expand All @@ -111,34 +110,41 @@ export default {
},
methods: {
login() {
/* global FB */
FB.login(
async response => {
try {
await this.$axios.post('/login', {
authResponse: response.authResponse
})
this.$store.dispatch('updateUserState')
this.$router.replace('/')
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
this.$store.dispatch('updateUserState')
this.$nuxt.error({ statusCode: 401 })
} else if (
code === 403 &&
e.response.data.reason === 'unregistered'
) {
if (process.env.NODE_ENV === 'production') {
/* global FB */
FB.login(
async response => {
try {
await this.$axios.post('/login', {
authResponse: response.authResponse
})
this.$store.dispatch('updateUserState')
this.$router.push('/signup')
} else {
throw e
this.$router.replace('/')
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
this.$store.dispatch('updateUserState')
this.$nuxt.error({ statusCode: 401 })
} else if (
code === 403 &&
e.response.data.reason === 'unregistered'
) {
this.$store.dispatch('updateUserState')
this.$router.push('/signup')
} else {
throw e
}
}
}
},
{ scope: 'public_profile,email' }
)
}
},
{ scope: 'public_profile,email' }
)
} else {
this.showLoginDialog()
}
},
...mapMutations({
showLoginDialog: 'showLoginDialog'
})
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SERVER_CONFIG as SV_CONFIG } from '../config/config.js'
* @async
*/
async function fakeFacebookLogin (ctx) {
let uid = ctx.query.user_id
let uid = ctx.request.body.user_id

if (!uid) {
consola.error('Got no user_id in query.')
Expand Down
133 changes: 67 additions & 66 deletions store/index.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
import Vuex from 'vuex'
export const state = () => ({
loggedIn: false,
userInfo: undefined,
dialogLoginVisible: false
})

const createStore = () => {
return new Vuex.Store({
state: () => ({
loggedIn: false,
userInfo: undefined
}),
mutations: {
setLoggedIn(state, loggedIn) {
state.loggedIn = loggedIn
},
setUserInfo(state, userInfo) {
state.userInfo = userInfo
export const mutations = {
setLoggedIn(state, loggedIn) {
state.loggedIn = loggedIn
},
setUserInfo(state, userInfo) {
state.userInfo = userInfo
},
showLoginDialog (state) {
state.dialogLoginVisible = true
},
hideLoginDialog (state) {
state.dialogLoginVisible = false
}
}

export const actions = {
async nuxtServerInit({ dispatch }, { $axios, route }) {
try {
let { data } = await $axios.get('/user/information')
dispatch('setLoggedIn', true)
dispatch('setUserInfo', data)
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else if (code === 403) {
dispatch('setLoggedIn', true)
dispatch('setUserInfo', undefined)
} else if (code === 404) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else {
throw e
}
},
actions: {
async nuxtServerInit({ dispatch }, { $axios, route }) {
try {
let { data } = await $axios.get('/user/information')
dispatch('setLoggedIn', true)
dispatch('setUserInfo', data)
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else if (code === 403) {
dispatch('setLoggedIn', true)
dispatch('setUserInfo', undefined)
} else if (code === 404) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else {
throw e
}
}
},
setLoggedIn({ commit }, loggedIn) {
commit('setLoggedIn', loggedIn)
},
setUserInfo({ commit }, userInfo) {
commit('setUserInfo', userInfo)
},
async updateUserState({ dispatch }) {
try {
let { data } = await this.$axios.get('/user/information')
dispatch('setLoggedIn', true)
dispatch('setUserInfo', data)
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else if (code === 403) {
dispatch('setLoggedIn', true)
dispatch('setUserInfo', undefined)
} else if (code === 404) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else {
throw e
}
}
}
},
setLoggedIn({ commit }, loggedIn) {
commit('setLoggedIn', loggedIn)
},
setUserInfo({ commit }, userInfo) {
commit('setUserInfo', userInfo)
},
async updateUserState({ dispatch }) {
try {
let { data } = await this.$axios.get('/user/information')
dispatch('setLoggedIn', true)
dispatch('setUserInfo', data)
} catch (e) {
const code = parseInt(e.response && e.response.status)
if (code === 401) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else if (code === 403) {
dispatch('setLoggedIn', true)
dispatch('setUserInfo', undefined)
} else if (code === 404) {
dispatch('setLoggedIn', false)
dispatch('setUserInfo', undefined)
} else {
throw e
}
}
})
}

export default createStore
}
}

0 comments on commit 5c938d0

Please sign in to comment.