upd: token device

Deskripsi
This commit is contained in:
amel
2025-06-24 17:40:29 +08:00
parent c6635fb88e
commit 720a8ce5fb
4 changed files with 73 additions and 23 deletions

View File

@@ -1,17 +1,24 @@
import ViewLogin from "@/components/auth/viewLogin";
import ViewVerification from "@/components/auth/viewVerification";
import { requestPermission } from "@/lib/useNotification";
import { useAuthSession } from "@/providers/AuthProvider";
import { Redirect } from "expo-router";
import { useState } from "react";
import { useEffect, useState } from "react";
import { Text } from "react-native";
import { useNotification } from "@/lib/useNotification";
export default function Index() {
useNotification()
const [isValid, setValid] = useState(false)
const [phone, setPhone] = useState('')
const [otp, setOtp] = useState(0)
async function registerNotification() {
const permission = await requestPermission()
}
useEffect(() => {
registerNotification()
}, [])
const { token, isLoading } = useAuthSession()
if (isLoading) {
return <Text>Loading...</Text>;

View File

@@ -2,7 +2,8 @@ import axios from 'axios';
const api = axios.create({
// baseURL: 'http://10.0.2.2:3000/api',
baseURL: 'https://stg-darmasaba.wibudev.com/api',
// baseURL: 'https://stg-darmasaba.wibudev.com/api',
baseURL: 'http://192.168.1.243:3000/api',
});
export const apiCheckPhoneLogin = async (body: { phone: string }) => {
@@ -632,4 +633,14 @@ export const apiCopyDocument = async (data: { path: string, dataItem: any[], use
export const apiShareDocument = async (data: { dataDivision: any[], dataItem: any[], user: string }) => {
const response = await api.delete(`/mobile/document/more`, { data })
return response.data;
};
export const apiRegisteredToken = async (data: { user: string, token: string }) => {
const response = await api.post(`/mobile/auth-token/`, data)
return response.data;
};
export const apiUnregisteredToken = async (data: { user: string, token: string }) => {
const response = await api.put(`/mobile/auth-token/`, data)
return response.data;
};

View File

@@ -32,22 +32,28 @@ const initializeFirebase = async () => {
}
};
const requestPermission = async () => {
export const requestPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
);
return granted;
const cek = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
if (!cek) {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
return true
}
return false
}
return true
} catch (err) {
console.warn('Error requesting notification permissions:', err);
}
};
const getToken = async () => {
export const getToken = async () => {
try {
const mess = await initializeFirebase();
const token = await mess?.getToken();
console.log('Token:', token);
return token;
} catch (error) {
console.error("Error getting token:", error);
@@ -59,7 +65,7 @@ export const useNotification = () => {
const initializeAndSetup = async () => {
try {
// await initializeFirebase();
await requestPermission();
// await requestPermission();
await getToken();
} catch (error) {
console.error('Failed to setup notifications:', error);

View File

@@ -1,7 +1,9 @@
import { apiRegisteredToken, apiUnregisteredToken } from '@/lib/api';
import { getToken, requestPermission } from '@/lib/useNotification';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {router} from "expo-router";
import {createContext, MutableRefObject, ReactNode, useCallback, useContext, useEffect, useRef, useState} from 'react';
import CryptoES from "crypto-es";
import { router } from "expo-router";
import { createContext, MutableRefObject, ReactNode, useCallback, useContext, useEffect, useRef, useState } from 'react';
const AuthContext = createContext<{
signIn: (arg0: string) => void;
@@ -24,12 +26,12 @@ export function useAuthSession() {
return useContext(AuthContext);
}
export default function AuthProvider ({children}:{children: ReactNode}): ReactNode {
const tokenRef = useRef<string|null>(null);
export default function AuthProvider({ children }: { children: ReactNode }): ReactNode {
const tokenRef = useRef<string | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
(async ():Promise<void> => {
(async (): Promise<void> => {
const token = await AsyncStorage.getItem('@token');
tokenRef.current = token || '';
setIsLoading(false);
@@ -49,15 +51,39 @@ export default function AuthProvider ({children}:{children: ReactNode}): ReactN
})
const signIn = useCallback(async (token: string) => {
await AsyncStorage.setItem('@token', token);
tokenRef.current = token;
router.replace('/home')
const hasil = await decryptToken(String(token))
const permission = await requestPermission()
if (permission) {
const tokenDevice = await getToken()
try {
const register = await apiRegisteredToken({ user: hasil, token: String(tokenDevice) })
} catch (error) {
console.error(error)
} finally {
await AsyncStorage.setItem('@token', token);
tokenRef.current = token;
router.replace('/home')
return true
}
} else {
await AsyncStorage.setItem('@token', token);
tokenRef.current = token;
router.replace('/home')
}
}, []);
const signOut = useCallback(async () => {
await AsyncStorage.setItem('@token', '');
tokenRef.current = null;
router.replace('/');
try {
const hasil = await decryptToken(String(tokenRef.current))
const token = await getToken()
const response = await apiUnregisteredToken({ user: hasil, token: String(token) })
} catch (error) {
console.error(error)
} finally {
await AsyncStorage.setItem('@token', '');
tokenRef.current = null;
router.replace('/');
}
}, []);
return (