forked from simmatrix/vue-google-auth
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sample.html
90 lines (80 loc) · 2.73 KB
/
sample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<template>
<div class="container container-table">
<!-- Errors -->
<div v-if=response class="text-red"><p>{{response}}</p></div>
<!-- login Button -->
<a id="signin-button" v-on:click="signIn">
<i class="fa fa-google-plus-official fa-3x"></i>
Sign in with Google
</a>
</div>
</template>
<script>
/** -----------------------------------------------------------------------------------------------------------
* You would first need to place these 3 lines of code in your APP ENTRY file, e.g. src/main.js
*
* import GoogleAuth from 'vue-google-oauth'
* Vue.use(GoogleAuth, { client_id: 'xxxxxxxx-xxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com' })
* Vue.googleAuth().load()
*
* -----------------------------------------------------------------------------------------------------------
*/
import Vue from 'vue'
module.exports = {
name: 'Login',
data: function (router) {
return {
section: 'Login',
loading: '',
response: ''
}
},
methods: {
signIn: function () {
Vue.googleAuth().signIn(this.onSignInSuccess, this.onSignInError)
},
onSignInSuccess: function (authorizationCode) {
this.toggleLoading()
this.resetResponse()
this.$http.post('http://your-backend-server.com/auth/google', { code: authorizationCode, redirect_uri: 'postmessage' }).then(function (response) {
if (response.body) {
var data = response.body
// Save to vuex
var token = 'Bearer ' + data.token
this.$store.commit('SET_USER', data.user_data)
this.$store.commit('SET_TOKEN', token)
// Save to local storage as well
// ( or you can install the vuex-persistedstate plugin so that you won't have to do this step, only store to Vuex is sufficient )
if (window.localStorage) {
window.localStorage.setItem('user', JSON.stringify(data.user_data))
window.localStorage.setItem('token', token)
}
// redirect to the dashboard
this.$router.push({ name: 'home' })
}
}, function (response) {
var data = response.body
this.response = data.error
console.log('BACKEND SERVER - SIGN-IN ERROR', data)
})
},
onSignInError: function (error) {
this.response = 'Failed to sign-in'
console.log('GOOGLE SERVER - SIGN-IN ERROR', error)
},
toggleLoading: function () {
this.loading = (this.loading === '') ? 'loading' : ''
},
resetResponse: function () {
this.response = ''
}
}
}
</script>
<style>
/**
* ----------------------------------------------------
* Styling? It's time to show your designing skills!
* ----------------------------------------------------
*/
</style>