Test: Google oauth
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
VITE_API_URL=https://localhost:5001/
|
VITE_API_URL=https://localhost:5001/
|
||||||
|
VITE_GOOGLE_CLIENT_ID=468391910875-78sfopq1t12ulrv4f5vj227j45guuj66.apps.googleusercontent.com
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
VITE_API_URL=todo
|
VITE_API_URL=todo
|
||||||
|
VITE_GOOGLE_CLIENT_ID=468391910875-78sfopq1t12ulrv4f5vj227j45guuj66.apps.googleusercontent.com
|
||||||
|
|||||||
9
package-lock.json
generated
9
package-lock.json
generated
@@ -14,6 +14,7 @@
|
|||||||
"pinia": "^2.1.7",
|
"pinia": "^2.1.7",
|
||||||
"vue": "^3.4.15",
|
"vue": "^3.4.15",
|
||||||
"vue-router": "^4.2.5",
|
"vue-router": "^4.2.5",
|
||||||
|
"vue3-google-login": "^2.0.26",
|
||||||
"vuetify": "^3.5.6"
|
"vuetify": "^3.5.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -3479,6 +3480,14 @@
|
|||||||
"vue": "^3.2.0"
|
"vue": "^3.2.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/vue3-google-login": {
|
||||||
|
"version": "2.0.26",
|
||||||
|
"resolved": "https://registry.npmjs.org/vue3-google-login/-/vue3-google-login-2.0.26.tgz",
|
||||||
|
"integrity": "sha512-BuTSIeSjINNHNPs+BDF4COnjWvff27IfCBDxK6JPRqvm57lF8iK4B3+zcG8ud6BXfZdyuiDlxletbEDgg4/RFA==",
|
||||||
|
"peerDependencies": {
|
||||||
|
"vue": "^3.0.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/vuetify": {
|
"node_modules/vuetify": {
|
||||||
"version": "3.5.6",
|
"version": "3.5.6",
|
||||||
"resolved": "https://registry.npmjs.org/vuetify/-/vuetify-3.5.6.tgz",
|
"resolved": "https://registry.npmjs.org/vuetify/-/vuetify-3.5.6.tgz",
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
"pinia": "^2.1.7",
|
"pinia": "^2.1.7",
|
||||||
"vue": "^3.4.15",
|
"vue": "^3.4.15",
|
||||||
"vue-router": "^4.2.5",
|
"vue-router": "^4.2.5",
|
||||||
|
"vue3-google-login": "^2.0.26",
|
||||||
"vuetify": "^3.5.6"
|
"vuetify": "^3.5.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -26,4 +27,4 @@
|
|||||||
"tailwindcss": "^3.4.1",
|
"tailwindcss": "^3.4.1",
|
||||||
"vite": "^5.0.11"
|
"vite": "^5.0.11"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { createVuetify } from 'vuetify'
|
|||||||
import * as components from 'vuetify/components'
|
import * as components from 'vuetify/components'
|
||||||
import * as directives from 'vuetify/directives'
|
import * as directives from 'vuetify/directives'
|
||||||
import clientPlugin from './plugins/api.js'
|
import clientPlugin from './plugins/api.js'
|
||||||
|
import vueGoogleOauth from 'vue3-google-login'
|
||||||
|
|
||||||
const vuetify = createVuetify({
|
const vuetify = createVuetify({
|
||||||
components,
|
components,
|
||||||
@@ -20,6 +21,9 @@ const app = createApp(App);
|
|||||||
// Create an axios client preconfigured to the Hutopy API.
|
// Create an axios client preconfigured to the Hutopy API.
|
||||||
app.use(clientPlugin);
|
app.use(clientPlugin);
|
||||||
|
|
||||||
|
app.use(vueGoogleOauth, {
|
||||||
|
clientId: import.meta.env.VITE_GOOGLE_CLIENT_ID,
|
||||||
|
})
|
||||||
app.use(createPinia());
|
app.use(createPinia());
|
||||||
app.use(vuetify);
|
app.use(vuetify);
|
||||||
app.use(router);
|
app.use(router);
|
||||||
|
|||||||
@@ -4,47 +4,64 @@ const baseUrl = '/api/Users';
|
|||||||
|
|
||||||
export const auth = defineStore({
|
export const auth = defineStore({
|
||||||
id: 'auth',
|
id: 'auth',
|
||||||
|
|
||||||
state: () => ({
|
state: () => ({
|
||||||
user: null,
|
user: "",
|
||||||
refreshTokenTimeout: null
|
refreshTokenTimeout: 0
|
||||||
}),
|
}),
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
|
async googleLogin(client, idToken) {
|
||||||
|
const response = await client.post("https://people.googleapis.com/v1/people/me", {
|
||||||
|
headers: {
|
||||||
|
"Authorization": "Bearer " + idToken
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.user = {
|
||||||
|
accessToken: response.data.accessToken,
|
||||||
|
refreshToken: response.data.refreshToken,
|
||||||
|
email: response.data.email
|
||||||
|
}
|
||||||
|
localStorage.setItem('jwt', this.user.accessToken);
|
||||||
|
this.startRefreshTokenTimer();
|
||||||
|
},
|
||||||
|
|
||||||
async login(client, email, password) {
|
async login(client, email, password) {
|
||||||
const requestBody = {
|
const requestBody = {
|
||||||
email: email,
|
email: email,
|
||||||
password: password
|
password: password
|
||||||
};
|
};
|
||||||
try {
|
const response = await client.post(`${baseUrl}/login`, requestBody)
|
||||||
const response = await client.post(`${baseUrl}/login`, requestBody)
|
this.user = {
|
||||||
this.user = {
|
accessToken: response.data.accessToken,
|
||||||
accessToken: response.data.accessToken,
|
refreshToken: response.data.refreshToken,
|
||||||
refreshToken: response.data.refreshToken,
|
email: email
|
||||||
email: email
|
|
||||||
}
|
|
||||||
localStorage.setItem('jwt', this.user.accessToken);
|
|
||||||
this.startRefreshTokenTimer();
|
|
||||||
} catch (error) {
|
|
||||||
throw new Error('Login failed.')
|
|
||||||
}
|
}
|
||||||
},
|
|
||||||
logout() {
|
|
||||||
localStorage.setItem('jwt', '');
|
|
||||||
this.stopRefreshTokenTimer();
|
|
||||||
this.user = null;
|
|
||||||
|
|
||||||
},
|
|
||||||
async refreshToken(client) {
|
|
||||||
const response = await client.post(`${baseUrl}/refresh`, {});
|
|
||||||
this.user.accessToken = response.accessToken;
|
|
||||||
localStorage.setItem('jwt', this.user.accessToken);
|
localStorage.setItem('jwt', this.user.accessToken);
|
||||||
|
|
||||||
|
|
||||||
this.startRefreshTokenTimer();
|
this.startRefreshTokenTimer();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
logout() {
|
||||||
|
localStorage.setItem('jwt', '');
|
||||||
|
this.user = null;
|
||||||
|
|
||||||
|
this.stopRefreshTokenTimer();
|
||||||
|
},
|
||||||
|
|
||||||
|
async refreshToken(client) {
|
||||||
|
const response = await client.post(`${baseUrl}/refresh`);
|
||||||
|
this.user.accessToken = response.accessToken;
|
||||||
|
localStorage.setItem('jwt', this.user.accessToken);
|
||||||
|
|
||||||
|
this.startRefreshTokenTimer();
|
||||||
|
},
|
||||||
|
|
||||||
startRefreshTokenTimer() {
|
startRefreshTokenTimer() {
|
||||||
const timeout = 50 * 1000;
|
const timeout = 50 * 1000;
|
||||||
this.refreshTokenTimeout = setTimeout(this.refreshToken, timeout);
|
this.refreshTokenTimeout = setTimeout(this.refreshToken, timeout);
|
||||||
},
|
},
|
||||||
|
|
||||||
stopRefreshTokenTimer() {
|
stopRefreshTokenTimer() {
|
||||||
clearTimeout(this.refreshTokenTimeout);
|
clearTimeout(this.refreshTokenTimeout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
<template>
|
<template>
|
||||||
<v-app style="background-color: #f4f4f4;">
|
<v-app style="background-color: #f4f4f4;">
|
||||||
|
<!-- Google Oauth -->
|
||||||
|
<GoogleLogin :callback="googleCallback" popup-type="TOKEN">
|
||||||
|
<button>Login Using Google</button>
|
||||||
|
</GoogleLogin>
|
||||||
<div class="sm:flex hidden items-center justify-between flex-col"
|
<div class="sm:flex hidden items-center justify-between flex-col"
|
||||||
style="background-color: #f4f4f4; margin-top: 3%;">
|
style="background-color: #f4f4f4; margin-top: 3%;">
|
||||||
|
|
||||||
@@ -101,6 +105,7 @@ import {auth} from '@/stores/auth.js';
|
|||||||
import {ref} from 'vue';
|
import {ref} from 'vue';
|
||||||
import {useRouter} from 'vue-router';
|
import {useRouter} from 'vue-router';
|
||||||
import {useClient} from "@/plugins/api.js";
|
import {useClient} from "@/plugins/api.js";
|
||||||
|
import {decodeCredential} from 'vue3-google-login'
|
||||||
|
|
||||||
const api = useClient()
|
const api = useClient()
|
||||||
|
|
||||||
@@ -119,6 +124,10 @@ async function login() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const googleCallback = (response) => {
|
||||||
|
console.log(response)
|
||||||
|
//store.loginWithGoogle(api, credential)
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -1218,4 +1218,4 @@ let items = [
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -8,7 +8,6 @@
|
|||||||
<v-container class="d-flex justify-center align-center">
|
<v-container class="d-flex justify-center align-center">
|
||||||
<div style="margin-left: 20%;">
|
<div style="margin-left: 20%;">
|
||||||
<v-row>
|
<v-row>
|
||||||
|
|
||||||
<img style=" max-width: 60vh; max-height: 11vh;" src="../../../images/hutopy.png">
|
<img style=" max-width: 60vh; max-height: 11vh;" src="../../../images/hutopy.png">
|
||||||
<RouterLink :to="{ name: 'login' }">
|
<RouterLink :to="{ name: 'login' }">
|
||||||
<v-btn size="large"
|
<v-btn size="large"
|
||||||
|
|||||||
Reference in New Issue
Block a user