Last active
February 17, 2025 17:46
-
-
Save bogordesaincom/973d904eec6704d6d9b25bb08d904f7b to your computer and use it in GitHub Desktop.
Contoh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| import Cookies from 'js-cookie'; | |
| import constantEndpoint from './constant'; | |
| const apiUrl = import.meta.env.VITE_API_ENDPOINT; | |
| const api = axios.create({ | |
| baseURL: apiUrl + '/' + constantEndpoint.apiVersion, | |
| headers: { | |
| 'X-Requested-With': 'XMLHttpRequest', | |
| 'X-localization': Cookies.get('locale'), | |
| }, | |
| withCredentials: false, | |
| }); | |
| api.interceptors.request.use( | |
| function (config) { | |
| const token = Cookies.get('inventori_token'); | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| return config; | |
| }, | |
| function (error) { | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export default api; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import api from '@/lib/api'; | |
| export class AuthService { | |
| login = async (value) => { | |
| return await api | |
| .post('/auth/login', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| loginGoogle = async (value) => { | |
| return await api | |
| .post('/auth/google_login', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| getMe = async () => { | |
| return await api | |
| .get('/auth/me') | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| completeSignup = async (value) => { | |
| return await api | |
| .post('/auth/complete_signup', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| logout = async (value) => { | |
| return await api | |
| .post('/auth/logout', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| register = async (value) => { | |
| return await api | |
| .post('/auth/register', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| activation = async (value) => { | |
| return await api | |
| .post('/auth/activation', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| forgot = async (value) => { | |
| return await api | |
| .post('/auth/forgot', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| reset = async (value) => { | |
| return await api | |
| .post('/auth/reset', value) | |
| .then((res) => res.data) | |
| .catch((err) => err.response); | |
| }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createContext, useContext } from 'react'; | |
| import api from '@/lib/api'; | |
| import { AuthService } from '@/services/auth-service'; | |
| import Cookies from 'js-cookie'; | |
| import useSWR from 'swr'; | |
| const AuthContext = createContext({}); | |
| const fetcher = (url) => api.get(url).then((res) => res.data); | |
| export const AuthProvider = ({ children }) => { | |
| const authService = new AuthService(); | |
| const { | |
| data: userData, | |
| isLoading, | |
| isValidating, | |
| error, | |
| } = useSWR('/auth/me', fetcher, { | |
| refreshInterval: false, | |
| revalidateIfStale: false, | |
| revalidateOnFocus: false, | |
| }); | |
| let userDataFinal = null; | |
| if (!isLoading || !isValidating || !error) { | |
| userDataFinal = { | |
| id: userData?.data?.id, | |
| name: userData?.data?.name, | |
| email: userData?.data?.email, | |
| username: userData?.data?.username, | |
| }; | |
| } | |
| // if (data?.data.token) { | |
| // Cookies.set('auth_token', data?.data.token, { | |
| // expires: 7, | |
| // secure: production ? true : false, | |
| // // domain: domain, | |
| // sameSite: 'strict', | |
| // }); | |
| // } | |
| const logout = async () => { | |
| localStorage.removeItem('menu_active'); | |
| localStorage.removeItem('submenu_active'); | |
| const { status } = await authService | |
| .logout() | |
| .then((res) => res) | |
| .catch((err) => err.response); | |
| if (status == 200) { | |
| Cookies.remove('inventori_token'); | |
| window.location.pathname = '/'; | |
| } else { | |
| Cookies.remove('inventori_token'); | |
| window.location.pathname = '/'; | |
| } | |
| }; | |
| return ( | |
| <AuthContext.Provider | |
| value={{ | |
| isLoading, | |
| user: userData ? userDataFinal : null, | |
| permissions: userData?.data.permissions, | |
| role: userData?.data.role_name, | |
| company_id: userData?.data.company_id, | |
| company_address: userData?.data.company_address, | |
| company_name: userData?.data.company_name, | |
| isValidating, | |
| error, | |
| logout, | |
| }} | |
| > | |
| {children} | |
| </AuthContext.Provider> | |
| ); | |
| }; | |
| export const useAuth = () => useContext(AuthContext); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const AdminBranchIndex = () => { | |
| const { t } = useTranslation(); | |
| const { permissions } = useAuth(); | |
| const { user } = useAuth(); | |
| const { role } = useAuth(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { AntdRegistry } from '@ant-design/nextjs-registry' | |
| import { Inter } from 'next/font/google' | |
| import React from 'react' | |
| import CompanyImage from '@/components/CompanyImage' | |
| import './globals.css' | |
| const inter = Inter({ subsets: ['latin'] }) | |
| export const metadata = { | |
| title: 'YVenture', | |
| description: 'Generated by YVenture', | |
| } | |
| export default function RootLayout({ children }: { children: React.ReactNode }) { | |
| return ( | |
| <html lang="en"> | |
| <head> | |
| <link rel="icon" href="/assets/images/company-image.png" /> | |
| </head> | |
| <body className={inter.className}> | |
| <AntdRegistry> <AuthProvider> | |
| {children} | |
| </AuthProvider> | |
| </AntdRegistry> | |
| </body> | |
| </html> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment