Skip to content

Commit

Permalink
Google Login
Browse files Browse the repository at this point in the history
  • Loading branch information
mudasirkk committed Dec 5, 2024
1 parent 8d92076 commit dcece62
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
19 changes: 15 additions & 4 deletions Client/src/components/LoginBadge.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
<script setup lang="ts">
import { useLogin } from '@/models/users'
import { refSession, useLogin } from '@/models/users'
const logins = useLogin
const logins = useLogin()
function googleLogin() {
logins.googleLogin()
}
const session = refSession()
</script>

<template>
<a class="button is-primary">
<strong>Sign up</strong>
</a>
<a class="button is-light" @click.prevent="googleLogin">
<a class="button is-light" @click.prevent="googleLogin" v-if="!session.user">
<i class="icon">
<img src="https://fontawesome.com/icons/google?f=brands&s=solid" alt="" />
<img
src="https://cdn-icons-png.flaticon.com/512/281/281764.png"
alt="Google Logo"
width="20"
/>
</i>
Google Login
</a>
<div v-if="session.user">
{{ session.user.firstName }}
{{ session.user.lastName }}
</div>
</template>

<style scoped></style>
1 change: 1 addition & 0 deletions Client/src/components/NavBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const isCartOpen = ref(false)

<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
<LoginBadge />
<button
class="button is-warning is-light is-active"
Expand Down
25 changes: 11 additions & 14 deletions Client/src/models/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { errorMessages } from "vue/compiler-sfc"
import { loadScript } from "./myFetch"
import { ref } from 'vue'
import { loadScript } from './myFetch'

export class User {
id?: number
Expand Down Expand Up @@ -37,27 +37,24 @@ const session = ref({

export const refSession = () => session

export const useLogin => ({
async login(email: string, password: string) {
},
export const useLogin = () => ({
async login(email: string, password: string) {},
async logout() {
session.value.user = null
},
async googleLogin() {
await loadScript('https://apis.google.com/js/api.js')
await loadScript('https://accounts.google.com/gsi/client')

const tokenClient = google.accounts.oauth2.initTokenClient({
client_id: 'import.meta.env.VITE_GOOGLE_CLIENT_ID',
scope: 'email',
callback: (response, any) => {
if(response.credential) {
session.value.token = response.credential
firstName = response.given_name,
lastName = response.family_name,
email = response.email
callback: (response: any) => {
console.log('response', response)
if (response.access_token) {
session.value.token = response.access_token
}
tokenClient.requestAccessToken({})
}
})
}

})
})

0 comments on commit dcece62

Please sign in to comment.