From 4d021a4634a6bc84ee25f0d43cbc6450d94265f0 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Mon, 31 Jul 2023 14:08:34 +0700 Subject: google sing up --- src/lib/auth/components/LoginDesktop.jsx | 11 +++++++++++ src/pages/_app.jsx | 8 +++++--- src/pages/api/auth/[...nextauth].js | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/pages/api/auth/[...nextauth].js (limited to 'src') diff --git a/src/lib/auth/components/LoginDesktop.jsx b/src/lib/auth/components/LoginDesktop.jsx index 8ce2e4e3..74d259a8 100644 --- a/src/lib/auth/components/LoginDesktop.jsx +++ b/src/lib/auth/components/LoginDesktop.jsx @@ -3,11 +3,15 @@ import useLogin from '../hooks/useLogin' import Link from '@/core/components/elements/Link/Link' import PageContent from '@/lib/content/components/PageContent' import Alert from '@/core/components/elements/Alert/Alert' +import {useSession, signIn, SignOut} from 'next-auth/react' const LoginDesktop = () => { const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = useLogin() + const {data : session } = useSession + console.log('ini google', session) + return (
@@ -60,6 +64,13 @@ const LoginDesktop = () => { {!isLoading ? 'Masuk' : 'Loading...'} +
Belum punya akun Indoteknik?{' '} diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index 4c4fed89..cda9e970 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -7,12 +7,14 @@ import { QueryClient, QueryClientProvider } from 'react-query' import useDevice from '@/core/hooks/useDevice' import { useEffect, useState } from 'react' import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' +import { SessionProvider } from 'next-auth/react' const queryClient = new QueryClient() -function MyApp({ Component, pageProps }) { +function MyApp({ Component, pageProps :{session, ...pageProps} }) { const router = useRouter() const { isMobile } = useDevice() + console.log('ini session', session) const [animateLoader, setAnimateLoader] = useState(false) @@ -47,7 +49,7 @@ function MyApp({ Component, pageProps }) { }, [isMobile]) return ( - <> + {animateLoader && ( } - + ) } diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js new file mode 100644 index 00000000..f1d6a31f --- /dev/null +++ b/src/pages/api/auth/[...nextauth].js @@ -0,0 +1,15 @@ +import NextAuth from "next-auth/next"; +import GoogleProvider from "next-auth/providers/google" + +export default NextAuth({ + providers:[ + GoogleProvider({ + clientId: process.env.GOOGLE_CLIENT_ID, + clientSecret: process.env.GOOGLE_CLIENT_SECRET, + }), + ], + secret:process.env.JWT_SECRET + // pages:{ + // signIn: '/login', + // } +}) \ No newline at end of file -- cgit v1.2.3 From 4cfe9157d64a76bf9913fe599d908497a18f5316 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Wed, 9 Aug 2023 10:45:53 +0700 Subject: google sign in --- src/lib/auth/components/LoginDesktop.jsx | 10 ++++---- src/pages/api/auth/[...nextauth].js | 40 +++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/lib/auth/components/LoginDesktop.jsx b/src/lib/auth/components/LoginDesktop.jsx index 74d259a8..f5aa0018 100644 --- a/src/lib/auth/components/LoginDesktop.jsx +++ b/src/lib/auth/components/LoginDesktop.jsx @@ -9,8 +9,8 @@ const LoginDesktop = () => { const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = useLogin() - const {data : session } = useSession - console.log('ini google', session) + const { data } = useSession() + console.log('ini google', data) return ( @@ -66,10 +66,10 @@ const LoginDesktop = () => {
diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js index f1d6a31f..6cc8a101 100644 --- a/src/pages/api/auth/[...nextauth].js +++ b/src/pages/api/auth/[...nextauth].js @@ -1,15 +1,29 @@ -import NextAuth from "next-auth/next"; -import GoogleProvider from "next-auth/providers/google" +import NextAuth from 'next-auth/next' +import GoogleProvider from 'next-auth/providers/google' export default NextAuth({ - providers:[ - GoogleProvider({ - clientId: process.env.GOOGLE_CLIENT_ID, - clientSecret: process.env.GOOGLE_CLIENT_SECRET, - }), - ], - secret:process.env.JWT_SECRET - // pages:{ - // signIn: '/login', - // } -}) \ No newline at end of file + providers: [ + GoogleProvider({ + clientId: process.env.GOOGLE_CLIENT_ID, + clientSecret: process.env.GOOGLE_CLIENT_SECRET + }) + ], + callbacks: { + async jwt({ token, account }) { + // Persist the OAuth access_token to the token right after signin + if (account) { + token.accessToken = account.access_token + } + return token + }, + async session({ session, token, user }) { + // Send properties to the client, like an access_token from a provider. + session.accessToken = token.accessToken + return session + } + }, + secret:process.env.JWT_SECRET + // pages:{ + // signIn: '/login', + // } +}) -- cgit v1.2.3 From 01e86fb4a54b801a9b8124455611c312e5de4af0 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Mon, 14 Aug 2023 15:04:45 +0700 Subject: google sign in --- .../components/elements/Navbar/NavbarDesktop.jsx | 5 +- .../elements/Navbar/NavbarUserDropdown.jsx | 8 +- src/core/components/layouts/BasicLayout.jsx | 93 +++++++++++++--------- src/core/utils/auth.js | 4 +- src/lib/auth/components/LoginDesktop.jsx | 66 ++++++++++++--- src/lib/auth/components/LoginMobile.jsx | 32 ++++++++ src/pages/api/auth/[...nextauth].js | 13 +-- src/pages/my/menu.jsx | 4 +- 8 files changed, 167 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/core/components/elements/Navbar/NavbarDesktop.jsx b/src/core/components/elements/Navbar/NavbarDesktop.jsx index ea4b03ae..c6575831 100644 --- a/src/core/components/elements/Navbar/NavbarDesktop.jsx +++ b/src/core/components/elements/Navbar/NavbarDesktop.jsx @@ -17,9 +17,10 @@ import { getCountCart } from '@/core/utils/cart' import TopBanner from './TopBanner' import whatsappUrl from '@/core/utils/whatsappUrl' import { useRouter } from 'next/router' -import { getAuth } from '@/core/utils/auth' +import { getAuth, setAuth } from '@/core/utils/auth' import { createSlug, getIdFromSlug } from '@/core/utils/slug' import productApi from '@/lib/product/api/productApi' +import { useSession } from 'next-auth/react' const Search = dynamic(() => import('./Search')) @@ -33,7 +34,7 @@ const NavbarDesktop = () => { const [payloadWA, setPayloadWa] = useState(null) const router = useRouter() - + useEffect(() => { const handleCartChange = () => { const cart = async () => { diff --git a/src/core/components/elements/Navbar/NavbarUserDropdown.jsx b/src/core/components/elements/Navbar/NavbarUserDropdown.jsx index 7848124c..b58be493 100644 --- a/src/core/components/elements/Navbar/NavbarUserDropdown.jsx +++ b/src/core/components/elements/Navbar/NavbarUserDropdown.jsx @@ -1,13 +1,15 @@ import { deleteAuth } from '@/core/utils/auth' import Link from '../Link/Link' import { useRouter } from 'next/router' +import { signOut, useSession } from 'next-auth/react' const NavbarUserDropdown = () => { const router = useRouter() - const logout = () => { - deleteAuth() - router.push('/login') + const logout = async () => { + deleteAuth().then(() => { + router.push('/login') + }) } return ( diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx index 073303fe..e8f6434b 100644 --- a/src/core/components/layouts/BasicLayout.jsx +++ b/src/core/components/layouts/BasicLayout.jsx @@ -6,10 +6,11 @@ import { useEffect, useState } from 'react' import axios from 'axios' import odooApi from '@/core/api/odooApi' import { useRouter } from 'next/router' -import { getURL } from 'next/dist/shared/lib/utils' import productApi from '@/lib/product/api/productApi' -import { getAuth } from '@/core/utils/auth' +import { getAuth, setAuth } from '@/core/utils/auth' import { createSlug, getIdFromSlug } from '@/core/utils/slug' +import { useSession } from 'next-auth/react' +import { setCookie } from 'cookies-next' const Navbar = dynamic(() => import('../elements/Navbar/Navbar')) const AnimationLayout = dynamic(() => import('./AnimationLayout')) @@ -17,10 +18,26 @@ const AnimationLayout = dynamic(() => import('./AnimationLayout')) const BasicLayout = ({ children }) => { const [templateWA, setTemplateWA] = useState(null) const [payloadWA, setPayloadWa] = useState(null) + const [isLoading, setIsloading] = useState(false) const router = useRouter() + const { data: session } = useSession() + const auth = getAuth() + const setting = async () => { + if (!auth && session) { + setCookie('auth', JSON.stringify(session?.odooUser)) + setIsloading(false) + } + } + + useEffect(() => { + setting() + }, [session]) useEffect(() => { + setting() + console.log('ini session', session) + console.log('ini auth', getAuth()) const getIP = async () => { const ip = await odooApi('GET', '/api/ip-address') const data = { @@ -46,42 +63,46 @@ const BasicLayout = ({ children }) => { } getProduct() setTemplateWA('product') - } }, []) - return ( - <> - - - {children} - - - - - ) + if(isLoading){ + + }else{ + return ( + <> + + + {children} + + + + + ) + } + } export default BasicLayout diff --git a/src/core/utils/auth.js b/src/core/utils/auth.js index cddff2b8..03b20ae2 100644 --- a/src/core/utils/auth.js +++ b/src/core/utils/auth.js @@ -1,4 +1,5 @@ import { deleteCookie, getCookie, setCookie } from 'cookies-next' +import { signOut } from 'next-auth/react' /** * Retrieves authentication data from cookie and returns it as an object. @@ -27,7 +28,8 @@ const setAuth = (user) => { * * @returns {boolean} - Returns `true`. */ -const deleteAuth = () => { +const deleteAuth = async() => { + await signOut() deleteCookie('auth') return true } diff --git a/src/lib/auth/components/LoginDesktop.jsx b/src/lib/auth/components/LoginDesktop.jsx index f5aa0018..e3bff492 100644 --- a/src/lib/auth/components/LoginDesktop.jsx +++ b/src/lib/auth/components/LoginDesktop.jsx @@ -3,15 +3,50 @@ import useLogin from '../hooks/useLogin' import Link from '@/core/components/elements/Link/Link' import PageContent from '@/lib/content/components/PageContent' import Alert from '@/core/components/elements/Alert/Alert' -import {useSession, signIn, SignOut} from 'next-auth/react' +import { useSession, signIn, SignOut } from 'next-auth/react' +import Image from 'next/image' +import { useRouter } from 'next/router' +import { useEffect, useState } from 'react' +import Spinner from '@/core/components/elements/Spinner/Spinner' const LoginDesktop = () => { const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = useLogin() - const { data } = useSession() - console.log('ini google', data) + const router = useRouter() + const [query, setQuery] = useState(router?.query?.next || '/') + const { data: session } = useSession() + const [googleLoading, setGoogleloading] = useState(true) + useEffect(() => { + // Simulate loading for 2 seconds + const delay = setTimeout(() => { + setGoogleloading(false); + }, 3000); + + return () => { + clearTimeout(delay); // Clear the timeout if the component unmounts + }; + }, []); + + + const handleGoogle = async () => { + await signIn('google', { callbackUrl: '/login' }) + } + + useEffect(() => { + if (session) { + router.push(query) + } + }, [session, query]) + + if (googleLoading) { + return ( +
+ +
+ ) + } return (
@@ -64,13 +99,26 @@ const LoginDesktop = () => { {!isLoading ? 'Masuk' : 'Loading...'} +
+
+

ATAU

+
+
+ + 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()} + > + google image +

Masuk dengan Google

+
Belum punya akun Indoteknik?{' '} diff --git a/src/lib/auth/components/LoginMobile.jsx b/src/lib/auth/components/LoginMobile.jsx index b365330b..c5177625 100644 --- a/src/lib/auth/components/LoginMobile.jsx +++ b/src/lib/auth/components/LoginMobile.jsx @@ -5,10 +5,22 @@ import Alert from '@/core/components/elements/Alert/Alert' import MobileView from '@/core/components/views/MobileView' import useLogin from '../hooks/useLogin' +import { useSession, signIn, SignOut } from 'next-auth/react' +import { useRouter } from 'next/router' +import { useState } from 'react' + const LoginMobile = () => { const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = useLogin() + const router = useRouter() + const [query, setQuery] = useState(router?.query?.next || '/') + const { data: session } = useSession() + + if (session) { + router.push(query) + } + return (
@@ -56,6 +68,26 @@ const LoginMobile = () => { {!isLoading ? 'Masuk' : 'Loading...'} +
+
+

ATAU

+
+
+ +
Belum punya akun Indoteknik?{' '} diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js index 6cc8a101..b11ed097 100644 --- a/src/pages/api/auth/[...nextauth].js +++ b/src/pages/api/auth/[...nextauth].js @@ -1,3 +1,4 @@ +import odooApi from '@/core/api/odooApi' import NextAuth from 'next-auth/next' import GoogleProvider from 'next-auth/providers/google' @@ -10,20 +11,20 @@ export default NextAuth({ ], callbacks: { async jwt({ token, account }) { - // Persist the OAuth access_token to the token right after signin if (account) { token.accessToken = account.access_token } return token }, async session({ session, token, user }) { - // Send properties to the client, like an access_token from a provider. session.accessToken = token.accessToken + const params = { + access_token: session.accessToken + } + const data = await odooApi('POST', '/api/v1/user/validate-sso', params) + session.odooUser = data.user return session } }, - secret:process.env.JWT_SECRET - // pages:{ - // signIn: '/login', - // } + secret: process.env.JWT_SECRET }) diff --git a/src/pages/my/menu.jsx b/src/pages/my/menu.jsx index c8e1e7e9..bd20e2eb 100644 --- a/src/pages/my/menu.jsx +++ b/src/pages/my/menu.jsx @@ -5,11 +5,13 @@ import useAuth from '@/core/hooks/useAuth' import { deleteAuth } from '@/core/utils/auth' import IsAuth from '@/lib/auth/components/IsAuth' import { ChevronRightIcon, UserIcon } from '@heroicons/react/24/solid' +import { signOut, useSession } from 'next-auth/react' import { useRouter } from 'next/router' export default function Menu() { const auth = useAuth() const router = useRouter() + const {data : session} = useSession() const logout = () => { deleteAuth() @@ -62,7 +64,7 @@ export default function Menu() { Daftar Alamat
-
+
(logout, signOut)} className='p-4 mt-2'>
-- cgit v1.2.3 From 9a33f3a391a402807cc5e7913b1a97e430a7aaa2 Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Tue, 15 Aug 2023 15:16:38 +0700 Subject: sign in --- .../components/elements/Navbar/NavbarDesktop.jsx | 10 ++- src/core/components/layouts/BasicLayout.jsx | 87 ++++++++++------------ src/lib/auth/components/LoginDesktop.jsx | 35 +++------ src/pages/_app.jsx | 87 +++++++++++++--------- 4 files changed, 104 insertions(+), 115 deletions(-) (limited to 'src') diff --git a/src/core/components/elements/Navbar/NavbarDesktop.jsx b/src/core/components/elements/Navbar/NavbarDesktop.jsx index c6575831..06c15b73 100644 --- a/src/core/components/elements/Navbar/NavbarDesktop.jsx +++ b/src/core/components/elements/Navbar/NavbarDesktop.jsx @@ -10,7 +10,7 @@ import DesktopView from '../../views/DesktopView' import dynamic from 'next/dynamic' import IndoteknikLogo from '@/images/logo.png' import Category from '@/lib/category/components/Category' -import { useEffect, useState } from 'react' +import { useContext, useEffect, useState } from 'react' import useAuth from '@/core/hooks/useAuth' import NavbarUserDropdown from './NavbarUserDropdown' import { getCountCart } from '@/core/utils/cart' @@ -21,11 +21,13 @@ import { getAuth, setAuth } from '@/core/utils/auth' import { createSlug, getIdFromSlug } from '@/core/utils/slug' import productApi from '@/lib/product/api/productApi' import { useSession } from 'next-auth/react' +import { AuthContext } from '@/pages/_app' const Search = dynamic(() => import('./Search')) const NavbarDesktop = () => { const [isOpenCategory, setIsOpenCategory] = useState(false) + const {authenticated} = useContext(AuthContext) const auth = useAuth() const [cartCount, setCartCount] = useState(0) @@ -177,7 +179,7 @@ const NavbarDesktop = () => {
- {!auth && ( + {!authenticated && ( <> { )} - {auth && ( + {authenticated && ( <>
- Halo, {auth?.name} + Halo, {authenticated?.name}
diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx index e8f6434b..e5c6908a 100644 --- a/src/core/components/layouts/BasicLayout.jsx +++ b/src/core/components/layouts/BasicLayout.jsx @@ -18,26 +18,20 @@ const AnimationLayout = dynamic(() => import('./AnimationLayout')) const BasicLayout = ({ children }) => { const [templateWA, setTemplateWA] = useState(null) const [payloadWA, setPayloadWa] = useState(null) - const [isLoading, setIsloading] = useState(false) const router = useRouter() const { data: session } = useSession() const auth = getAuth() const setting = async () => { - if (!auth && session) { - setCookie('auth', JSON.stringify(session?.odooUser)) - setIsloading(false) - } + setCookie('auth', JSON.stringify(session?.odooUser)) } useEffect(() => { - setting() - }, [session]) - - useEffect(() => { - setting() + // if (!auth && session) { + // setting() + // } + console.log('ini auth', auth) console.log('ini session', session) - console.log('ini auth', getAuth()) const getIP = async () => { const ip = await odooApi('GET', '/api/ip-address') const data = { @@ -65,44 +59,39 @@ const BasicLayout = ({ children }) => { setTemplateWA('product') } }, []) - if(isLoading){ - - }else{ - return ( - <> - - - {children} - - - - - ) - } - + return ( + <> + + + {children} + + + + + ) } export default BasicLayout diff --git a/src/lib/auth/components/LoginDesktop.jsx b/src/lib/auth/components/LoginDesktop.jsx index e3bff492..06c90d45 100644 --- a/src/lib/auth/components/LoginDesktop.jsx +++ b/src/lib/auth/components/LoginDesktop.jsx @@ -6,8 +6,10 @@ import Alert from '@/core/components/elements/Alert/Alert' import { useSession, signIn, SignOut } from 'next-auth/react' import Image from 'next/image' import { useRouter } from 'next/router' -import { useEffect, useState } from 'react' +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' const LoginDesktop = () => { const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = @@ -16,37 +18,20 @@ const LoginDesktop = () => { const router = useRouter() const [query, setQuery] = useState(router?.query?.next || '/') const { data: session } = useSession() - const [googleLoading, setGoogleloading] = useState(true) - - useEffect(() => { - // Simulate loading for 2 seconds - const delay = setTimeout(() => { - setGoogleloading(false); - }, 3000); - - return () => { - clearTimeout(delay); // Clear the timeout if the component unmounts - }; - }, []); - + const auth = getAuth() + const { setAuthenticated } = useContext(AuthContext) const handleGoogle = async () => { - await signIn('google', { callbackUrl: '/login' }) + const url = query != '/' ? '/login?next=' + query : '/login' + await signIn('google', { callbackUrl: url }) } useEffect(() => { - if (session) { - router.push(query) + if (session || auth) { + setAuthenticated(session ? session.odooUser : auth) } - }, [session, query]) + }, [session]) - if (googleLoading) { - return ( -
- -
- ) - } return (
diff --git a/src/pages/_app.jsx b/src/pages/_app.jsx index cda9e970..9f30752a 100644 --- a/src/pages/_app.jsx +++ b/src/pages/_app.jsx @@ -5,18 +5,29 @@ import { AnimatePresence, motion } from 'framer-motion' import { Toaster } from 'react-hot-toast' import { QueryClient, QueryClientProvider } from 'react-query' import useDevice from '@/core/hooks/useDevice' -import { useEffect, useState } from 'react' +import { createContext, useContext, useEffect, useState } from 'react' import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' import { SessionProvider } from 'next-auth/react' +import { getAuth } from '@/core/utils/auth' const queryClient = new QueryClient() -function MyApp({ Component, pageProps :{session, ...pageProps} }) { +export const AuthContext = createContext({ + authenticated : false, + setAuthenticated : (auth) => {} +}) + +function MyApp({ Component, pageProps: { session, ...pageProps } }) { const router = useRouter() const { isMobile } = useDevice() - console.log('ini session', session) const [animateLoader, setAnimateLoader] = useState(false) + const [authenticated, setAuthenticated] = useState(null) + const auth = getAuth() + + useEffect(() => { + setAuthenticated(auth) + }, [auth]) useEffect(() => { const handleRouteChangeStart = () => setAnimateLoader(true) @@ -49,41 +60,43 @@ function MyApp({ Component, pageProps :{session, ...pageProps} }) { }, [isMobile]) return ( - - - {animateLoader && ( - - - - )} - - - - - window.scrollTo(0, 0)} - > - {!animateLoader && } + + + + {animateLoader && ( + + + + )} - - + + + + window.scrollTo(0, 0)} + > + {!animateLoader && } + + + + ) } -- cgit v1.2.3 From f01f28a7eac76c6da5bf857bfc80fd347586ce7f Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Wed, 16 Aug 2023 09:39:47 +0700 Subject: register google --- .../components/elements/Navbar/NavbarDesktop.jsx | 6 ++-- src/core/components/layouts/BasicLayout.jsx | 6 ++-- src/lib/auth/components/LoginDesktop.jsx | 11 +++--- src/lib/auth/components/LoginMobile.jsx | 17 +++++++--- src/lib/auth/components/RegisterDesktop.jsx | 39 ++++++++++++++++++++++ src/lib/auth/components/RegisterMobile.jsx | 39 ++++++++++++++++++++++ src/pages/_app.jsx | 10 +++--- src/pages/my/menu.jsx | 9 ++--- 8 files changed, 112 insertions(+), 25 deletions(-) (limited to 'src') 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 = () => {
- {!authenticated && ( + {!auth && ( <> { )} - {authenticated && ( + {auth && ( <>
- Halo, {authenticated?.name} + Halo, {auth?.name}
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 ( 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 ( @@ -77,7 +86,7 @@ const LoginMobile = () => {
+
+
+

ATAU

+
+
+ +
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 (
@@ -122,6 +140,27 @@ const RegisterMobile = () => {
+
+
+

ATAU

+
+
+ + +
Sudah punya akun Indoteknik?{' '} 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 ( - + // {animateLoader && ( @@ -96,7 +96,7 @@ function MyApp({ Component, pageProps: { session, ...pageProps } }) { - + // ) } 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() { Daftar Alamat
-
(logout, signOut)} className='p-4 mt-2'> +
logout()} className='p-4 mt-2'>
-- cgit v1.2.3 From b6f6bf23f90ff0dfadf9bf0af8866c2cfc17aa9c Mon Sep 17 00:00:00 2001 From: "HATEC\\SPVDEV001" Date: Fri, 18 Aug 2023 11:02:39 +0700 Subject: handling redirect login --- src/core/components/layouts/BasicLayout.jsx | 10 ---------- src/lib/auth/components/LoginDesktop.jsx | 28 ++++++++++++++++++++-------- src/lib/auth/components/LoginMobile.jsx | 24 +++++++++++++++++++----- src/lib/auth/components/RegisterDesktop.jsx | 14 +++++++++++++- src/lib/auth/components/RegisterMobile.jsx | 13 ++++++++++++- src/lib/auth/hooks/useLogin.js | 19 ++++++++++++++++++- src/pages/api/auth/[...nextauth].js | 6 +----- 7 files changed, 83 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/core/components/layouts/BasicLayout.jsx b/src/core/components/layouts/BasicLayout.jsx index 0fea1a3b..272440c2 100644 --- a/src/core/components/layouts/BasicLayout.jsx +++ b/src/core/components/layouts/BasicLayout.jsx @@ -20,18 +20,8 @@ const BasicLayout = ({ children }) => { const [payloadWA, setPayloadWa] = useState(null) const router = useRouter() - const { data: session } = useSession() - const auth = getAuth() - const setting = async () => { - setCookie('auth', JSON.stringify(session?.odooUser)) - } useEffect(() => { - if (!auth && session) { - setting() - } - console.log('ini auth', auth) - console.log('ini session', session) const getIP = async () => { const ip = await odooApi('GET', '/api/ip-address') const data = { diff --git a/src/lib/auth/components/LoginDesktop.jsx b/src/lib/auth/components/LoginDesktop.jsx index 3a7477b9..42fa8896 100644 --- a/src/lib/auth/components/LoginDesktop.jsx +++ b/src/lib/auth/components/LoginDesktop.jsx @@ -9,27 +9,39 @@ import { useRouter } from 'next/router' import { useContext, useEffect, useState } from 'react' import { getAuth, setAuth } from '@/core/utils/auth' import { setCookie } from 'cookies-next' +import BottomPopup from '@/core/components/elements/Popup/BottomPopup' +import Spinner from '@/core/components/elements/Spinner/Spinner' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' +import odooApi from '@/core/api/odooApi' const LoginDesktop = () => { - const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = + const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef, handleGoogleSubmit } = useLogin() const router = useRouter() const [query, setQuery] = useState(router?.query?.next || '/') const { data: session } = useSession() - const auth = getAuth() const handleGoogle = async () => { - const url = query != '/' ? '/login?next=' + query : '/login' + const url = query != '/' ? '/login?source=google&next=' + query : '/login?source=google' await signIn('google', { callbackUrl: url }) } - useEffect(() => { - if(session){ - setCookie('auth', JSON.stringify(session?.odooUser)) - router.push(query) + if (session) { + handleGoogleSubmit(session) } - },[session]) + }, [session]) + + if (router.query.source) { + return ( + +
Mohon Tunggu
+
+ +
+
+ ) + } return ( diff --git a/src/lib/auth/components/LoginMobile.jsx b/src/lib/auth/components/LoginMobile.jsx index 68ceb769..095f4174 100644 --- a/src/lib/auth/components/LoginMobile.jsx +++ b/src/lib/auth/components/LoginMobile.jsx @@ -9,27 +9,41 @@ import { useSession, signIn, SignOut } from 'next-auth/react' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' import { setCookie } from 'cookies-next' +import BottomPopup from '@/core/components/elements/Popup/BottomPopup' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' +import odooApi from '@/core/api/odooApi' +import { getAuth } from '@/core/utils/auth' const LoginMobile = () => { - const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef } = + const { handleSubmit, handleChangeInput, isLoading, isValid, alert, emailRef, passwordRef, handleGoogleSubmit } = useLogin() const router = useRouter() const [query, setQuery] = useState(router?.query?.next || '/') const { data: session } = useSession() + const auth = getAuth() const handleGoogle = async () => { - const url = query != '/' ? '/login?next=' + query : '/login' + const url = query != '/' ? '/login?source=google&next=' + query : '/login?source=google' await signIn('google', { callbackUrl: url }) - setCookie('auth', JSON.stringify(session?.odooUser)) } useEffect(() => { if (session) { - setCookie('auth', JSON.stringify(session?.odooUser)) - router.push(query) + handleGoogleSubmit(session) } }, [session]) + if (router.query.source) { + return ( + +
Mohon Tunggu
+
+ +
+
+ ) + } + return (
diff --git a/src/lib/auth/components/RegisterDesktop.jsx b/src/lib/auth/components/RegisterDesktop.jsx index 86a5027d..a2bc98c2 100644 --- a/src/lib/auth/components/RegisterDesktop.jsx +++ b/src/lib/auth/components/RegisterDesktop.jsx @@ -10,6 +10,7 @@ import { useEffect } from 'react' import { setCookie } from 'cookies-next' import { signIn, useSession } from 'next-auth/react' import { useRouter } from 'next/router' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' const RegisterDesktop = () => { const { @@ -31,7 +32,7 @@ const RegisterDesktop = () => { const router = useRouter() const handleGoogle = async () => { - await signIn('google', { callbackUrl: '/register' }) + await signIn('google', { callbackUrl: '/register?source=google' }) } useEffect(() => { @@ -41,6 +42,17 @@ const RegisterDesktop = () => { } },[session]) + if (router.query.source) { + return ( + +
Mohon Tunggu
+
+ +
+
+ ) + } + return (
diff --git a/src/lib/auth/components/RegisterMobile.jsx b/src/lib/auth/components/RegisterMobile.jsx index c21c9325..2e918a14 100644 --- a/src/lib/auth/components/RegisterMobile.jsx +++ b/src/lib/auth/components/RegisterMobile.jsx @@ -11,6 +11,7 @@ import { signIn, useSession } from 'next-auth/react' import { useRouter } from 'next/router' import { setCookie } from 'cookies-next' import { useEffect } from 'react' +import LogoSpinner from '@/core/components/elements/Spinner/LogoSpinner' const RegisterMobile = () => { const { @@ -32,7 +33,7 @@ const RegisterMobile = () => { const router = useRouter() const handleGoogle = async () => { - await signIn('google', { callbackUrl: '/register' }) + await signIn('google', { callbackUrl: '/register?source=google' }) } useEffect(() => { @@ -42,6 +43,16 @@ const RegisterMobile = () => { } },[session]) + if (router.query.source) { + return ( + +
Mohon Tunggu
+
+ +
+
+ ) + } return (
diff --git a/src/lib/auth/hooks/useLogin.js b/src/lib/auth/hooks/useLogin.js index 1d5ff43d..34605614 100644 --- a/src/lib/auth/hooks/useLogin.js +++ b/src/lib/auth/hooks/useLogin.js @@ -3,6 +3,9 @@ import { setAuth } from '@/core/utils/auth' import { useRouter } from 'next/router' import { useRef, useState } from 'react' import loginApi from '../api/loginApi' +import odooApi from '@/core/api/odooApi' +import { setCookie } from 'cookies-next' +import { useSession } from 'next-auth/react' const useLogin = () => { const router = useRouter() @@ -60,6 +63,19 @@ const useLogin = () => { } } + const handleGoogleSubmit = async (session) => { + const params = { + access_token: session.accessToken + } + const data = await odooApi('POST', '/api/v1/user/validate-sso', params) + if (data.isAuth) { + session.odooUser = data.user + setCookie('auth', JSON.stringify(session?.odooUser)) + router.push(router?.query?.next ?? '/') + return + } + } + return { handleChangeInput, handleSubmit, @@ -67,7 +83,8 @@ const useLogin = () => { isValid, alert, emailRef, - passwordRef + passwordRef, + handleGoogleSubmit } } diff --git a/src/pages/api/auth/[...nextauth].js b/src/pages/api/auth/[...nextauth].js index b11ed097..3c433167 100644 --- a/src/pages/api/auth/[...nextauth].js +++ b/src/pages/api/auth/[...nextauth].js @@ -18,11 +18,7 @@ export default NextAuth({ }, async session({ session, token, user }) { session.accessToken = token.accessToken - const params = { - access_token: session.accessToken - } - const data = await odooApi('POST', '/api/v1/user/validate-sso', params) - session.odooUser = data.user + return session } }, -- cgit v1.2.3