summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-08-16 09:39:47 +0700
committerHATEC\SPVDEV001 <tri.susilo@altama.co.id>2023-08-16 09:39:47 +0700
commitf01f28a7eac76c6da5bf857bfc80fd347586ce7f (patch)
tree0e7b587eeb0162852565dfa97d3c35d1354d8c21
parent9a33f3a391a402807cc5e7913b1a97e430a7aaa2 (diff)
register google
-rw-r--r--src/core/components/elements/Navbar/NavbarDesktop.jsx6
-rw-r--r--src/core/components/layouts/BasicLayout.jsx6
-rw-r--r--src/lib/auth/components/LoginDesktop.jsx11
-rw-r--r--src/lib/auth/components/LoginMobile.jsx17
-rw-r--r--src/lib/auth/components/RegisterDesktop.jsx39
-rw-r--r--src/lib/auth/components/RegisterMobile.jsx39
-rw-r--r--src/pages/_app.jsx10
-rw-r--r--src/pages/my/menu.jsx9
8 files changed, 112 insertions, 25 deletions
diff --git a/src/core/components/elements/Navbar/NavbarDesktop.jsx b/src/core/components/elements/Navbar/NavbarDesktop.jsx
index 06c15b73..17aedeb6 100644
--- a/src/core/components/elements/Navbar/NavbarDesktop.jsx
+++ b/src/core/components/elements/Navbar/NavbarDesktop.jsx
@@ -179,7 +179,7 @@ const NavbarDesktop = () => {
</div>
<div className='w-3/12 flex gap-x-1 relative'>
- {!authenticated && (
+ {!auth && (
<>
<Link
href='/login'
@@ -195,10 +195,10 @@ const NavbarDesktop = () => {
</Link>
</>
)}
- {authenticated && (
+ {auth && (
<>
<div href='/' className='navbar-user-dropdown-button'>
- <span>Halo, {authenticated?.name}</span>
+ <span>Halo, {auth?.name}</span>
<div className='ml-auto'>
<ChevronDownIcon className='w-6' />
</div>
diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx
index e5c6908a..0fea1a3b 100644
--- a/src/core/components/layouts/BasicLayout.jsx
+++ b/src/core/components/layouts/BasicLayout.jsx
@@ -27,9 +27,9 @@ const BasicLayout = ({ children }) => {
}
useEffect(() => {
- // if (!auth && session) {
- // setting()
- // }
+ if (!auth && session) {
+ setting()
+ }
console.log('ini auth', auth)
console.log('ini session', session)
const getIP = async () => {
diff --git a/src/lib/auth/components/LoginDesktop.jsx b/src/lib/auth/components/LoginDesktop.jsx
index 06c90d45..3a7477b9 100644
--- a/src/lib/auth/components/LoginDesktop.jsx
+++ b/src/lib/auth/components/LoginDesktop.jsx
@@ -7,9 +7,8 @@ import { useSession, signIn, SignOut } from 'next-auth/react'
import Image from 'next/image'
import { useRouter } from 'next/router'
import { useContext, useEffect, useState } from 'react'
-import Spinner from '@/core/components/elements/Spinner/Spinner'
-import { AuthContext } from '@/pages/_app'
import { getAuth, setAuth } from '@/core/utils/auth'
+import { setCookie } from 'cookies-next'
const LoginDesktop = () => {
const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } =
@@ -19,7 +18,6 @@ const LoginDesktop = () => {
const [query, setQuery] = useState(router?.query?.next || '/')
const { data: session } = useSession()
const auth = getAuth()
- const { setAuthenticated } = useContext(AuthContext)
const handleGoogle = async () => {
const url = query != '/' ? '/login?next=' + query : '/login'
@@ -27,10 +25,11 @@ const LoginDesktop = () => {
}
useEffect(() => {
- if (session || auth) {
- setAuthenticated(session ? session.odooUser : auth)
+ if(session){
+ setCookie('auth', JSON.stringify(session?.odooUser))
+ router.push(query)
}
- }, [session])
+ },[session])
return (
<DesktopView>
diff --git a/src/lib/auth/components/LoginMobile.jsx b/src/lib/auth/components/LoginMobile.jsx
index c5177625..68ceb769 100644
--- a/src/lib/auth/components/LoginMobile.jsx
+++ b/src/lib/auth/components/LoginMobile.jsx
@@ -7,7 +7,8 @@ import useLogin from '../hooks/useLogin'
import { useSession, signIn, SignOut } from 'next-auth/react'
import { useRouter } from 'next/router'
-import { useState } from 'react'
+import { useEffect, useState } from 'react'
+import { setCookie } from 'cookies-next'
const LoginMobile = () => {
const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } =
@@ -17,9 +18,17 @@ const LoginMobile = () => {
const [query, setQuery] = useState(router?.query?.next || '/')
const { data: session } = useSession()
- if (session) {
- router.push(query)
+ const handleGoogle = async () => {
+ const url = query != '/' ? '/login?next=' + query : '/login'
+ await signIn('google', { callbackUrl: url })
+ setCookie('auth', JSON.stringify(session?.odooUser))
}
+ useEffect(() => {
+ if (session) {
+ setCookie('auth', JSON.stringify(session?.odooUser))
+ router.push(query)
+ }
+ }, [session])
return (
<MobileView>
@@ -77,7 +86,7 @@ const LoginMobile = () => {
<button
type='submit'
className='border border-gray-500 p-2 rounded-md hover:bg-gray-100 w-full mt-2 flex items-center justify-center gap-x-2'
- onClick={() => signIn('google', { callbackUrl: '/login' })}
+ onClick={() => handleGoogle()}
>
<Image
src='/images/icons8-google.svg'
diff --git a/src/lib/auth/components/RegisterDesktop.jsx b/src/lib/auth/components/RegisterDesktop.jsx
index 93b505ab..86a5027d 100644
--- a/src/lib/auth/components/RegisterDesktop.jsx
+++ b/src/lib/auth/components/RegisterDesktop.jsx
@@ -5,6 +5,11 @@ import Alert from '@/core/components/elements/Alert/Alert'
import PageContent from '@/lib/content/components/PageContent'
import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
import ReCAPTCHA from 'react-google-recaptcha'
+import Image from 'next/image'
+import { useEffect } from 'react'
+import { setCookie } from 'cookies-next'
+import { signIn, useSession } from 'next-auth/react'
+import { useRouter } from 'next/router'
const RegisterDesktop = () => {
const {
@@ -21,6 +26,20 @@ const RegisterDesktop = () => {
tnd,
setTnd
} = useRegister()
+
+ const { data: session } = useSession()
+ const router = useRouter()
+
+ const handleGoogle = async () => {
+ await signIn('google', { callbackUrl: '/register' })
+ }
+
+ useEffect(() => {
+ if(session){
+ setCookie('auth', JSON.stringify(session?.odooUser))
+ router.push('/')
+ }
+ },[session])
return (
<DesktopView>
@@ -121,6 +140,26 @@ const RegisterDesktop = () => {
<PageContent path='/register#tnd'></PageContent>
</BottomPopup>
</div>
+ <div className='flex items-center mt-3 mb-3'>
+ <hr className='flex-1' />
+ <p className='text-gray-400'>ATAU</p>
+ <hr className='flex-1' />
+ </div>
+
+ <button
+ type='submit'
+ className='border border-gray-500 p-2 rounded-md hover:bg-gray-100 w-full mt-2 flex items-center justify-center gap-x-2'
+ onClick={() => handleGoogle()}
+ >
+ <Image
+ src='/images/icons8-google.svg'
+ alt='google image'
+ className='h-7 w-7'
+ width={10}
+ height={10}
+ />
+ <p>Daftar dengan Google</p>
+ </button>
<div className='text-gray_r-11 mt-10'>
Sudah punya akun Indoteknik?{' '}
diff --git a/src/lib/auth/components/RegisterMobile.jsx b/src/lib/auth/components/RegisterMobile.jsx
index da6efaf5..c21c9325 100644
--- a/src/lib/auth/components/RegisterMobile.jsx
+++ b/src/lib/auth/components/RegisterMobile.jsx
@@ -7,6 +7,10 @@ import MobileView from '@/core/components/views/MobileView'
import BottomPopup from '@/core/components/elements/Popup/BottomPopup'
import PageContent from '@/lib/content/components/PageContent'
import ReCAPTCHA from 'react-google-recaptcha'
+import { signIn, useSession } from 'next-auth/react'
+import { useRouter } from 'next/router'
+import { setCookie } from 'cookies-next'
+import { useEffect } from 'react'
const RegisterMobile = () => {
const {
@@ -24,6 +28,20 @@ const RegisterMobile = () => {
setTnd
} = useRegister()
+ const { data: session } = useSession()
+ const router = useRouter()
+
+ const handleGoogle = async () => {
+ await signIn('google', { callbackUrl: '/register' })
+ }
+
+ useEffect(() => {
+ if(session){
+ setCookie('auth', JSON.stringify(session?.odooUser))
+ router.push('/')
+ }
+ },[session])
+
return (
<MobileView>
<div className='p-6 pt-10 flex flex-col items-center min-h-screen'>
@@ -122,6 +140,27 @@ const RegisterMobile = () => {
</BottomPopup>
</div>
+ <div className='flex items-center mt-3 mb-3'>
+ <hr className='flex-1' />
+ <p className='text-gray-400'>ATAU</p>
+ <hr className='flex-1' />
+ </div>
+
+ <button
+ type='submit'
+ className='border border-gray-500 p-2 rounded-md hover:bg-gray-100 w-full mt-2 flex items-center justify-center gap-x-2'
+ onClick={() => handleGoogle()}
+ >
+ <Image
+ src='/images/icons8-google.svg'
+ alt='google image'
+ className='h-7 w-7'
+ width={10}
+ height={10}
+ />
+ <p>Daftar dengan Google</p>
+ </button>
+
<div className='text-gray_r-11 mt-4'>
Sudah punya akun Indoteknik?{' '}
<Link href='/login' className='inline'>
diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx
index 9f30752a..86cbe963 100644
--- a/src/pages/_app.jsx
+++ b/src/pages/_app.jsx
@@ -25,9 +25,9 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) {
const [authenticated, setAuthenticated] = useState(null)
const auth = getAuth()
- useEffect(() => {
- setAuthenticated(auth)
- }, [auth])
+ // useEffect(() => {
+ // setAuthenticated(auth)
+ // }, [auth])
useEffect(() => {
const handleRouteChangeStart = () => setAnimateLoader(true)
@@ -60,7 +60,7 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) {
}, [isMobile])
return (
- <AuthContext.Provider value={{authenticated, setAuthenticated}}>
+ // <AuthContext.Provider value={{authenticated, setAuthenticated}}>
<SessionProvider session={session}>
<AnimatePresence>
{animateLoader && (
@@ -96,7 +96,7 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) {
</AnimatePresence>
</QueryClientProvider>
</SessionProvider>
- </AuthContext.Provider>
+ // </AuthContext.Provider>
)
}
diff --git a/src/pages/my/menu.jsx b/src/pages/my/menu.jsx
index bd20e2eb..fb8e6b03 100644
--- a/src/pages/my/menu.jsx
+++ b/src/pages/my/menu.jsx
@@ -11,11 +11,12 @@ import { useRouter } from 'next/router'
export default function Menu() {
const auth = useAuth()
const router = useRouter()
- const {data : session} = useSession()
+ const { data: session } = useSession()
const logout = () => {
- deleteAuth()
- router.push('/login')
+ deleteAuth().then(() => {
+ router.push('/login')
+ })
}
return (
@@ -64,7 +65,7 @@ export default function Menu() {
<LinkItem href='/my/address'>Daftar Alamat</LinkItem>
</div>
- <div onClick={() => (logout, signOut)} className='p-4 mt-2'>
+ <div onClick={() => logout()} className='p-4 mt-2'>
<button className='w-full btn-red'>Keluar Akun</button>
</div>
</div>