upd: token device
Deskripsi
This commit is contained in:
@@ -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>;
|
||||
|
||||
13
lib/api.ts
13
lib/api.ts
@@ -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 }) => {
|
||||
@@ -633,3 +634,13 @@ export const apiShareDocument = async (data: { dataDivision: any[], dataItem: an
|
||||
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;
|
||||
};
|
||||
@@ -32,22 +32,28 @@ const initializeFirebase = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const requestPermission = async () => {
|
||||
export const requestPermission = async () => {
|
||||
try {
|
||||
const cek = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
|
||||
if (!cek) {
|
||||
const granted = await PermissionsAndroid.request(
|
||||
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
||||
);
|
||||
return granted;
|
||||
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);
|
||||
|
||||
@@ -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 CryptoES from "crypto-es";
|
||||
import { router } from "expo-router";
|
||||
import { createContext, MutableRefObject, ReactNode, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||
import CryptoES from "crypto-es";
|
||||
|
||||
const AuthContext = createContext<{
|
||||
signIn: (arg0: string) => void;
|
||||
@@ -49,15 +51,39 @@ export default function AuthProvider ({children}:{children: ReactNode}): ReactN
|
||||
})
|
||||
|
||||
const signIn = useCallback(async (token: string) => {
|
||||
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 () => {
|
||||
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 (
|
||||
|
||||
Reference in New Issue
Block a user