upd: token device
Deskripsi
This commit is contained in:
@@ -1,17 +1,24 @@
|
|||||||
import ViewLogin from "@/components/auth/viewLogin";
|
import ViewLogin from "@/components/auth/viewLogin";
|
||||||
import ViewVerification from "@/components/auth/viewVerification";
|
import ViewVerification from "@/components/auth/viewVerification";
|
||||||
|
import { requestPermission } from "@/lib/useNotification";
|
||||||
import { useAuthSession } from "@/providers/AuthProvider";
|
import { useAuthSession } from "@/providers/AuthProvider";
|
||||||
import { Redirect } from "expo-router";
|
import { Redirect } from "expo-router";
|
||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Text } from "react-native";
|
import { Text } from "react-native";
|
||||||
import { useNotification } from "@/lib/useNotification";
|
|
||||||
|
|
||||||
export default function Index() {
|
export default function Index() {
|
||||||
useNotification()
|
|
||||||
const [isValid, setValid] = useState(false)
|
const [isValid, setValid] = useState(false)
|
||||||
const [phone, setPhone] = useState('')
|
const [phone, setPhone] = useState('')
|
||||||
const [otp, setOtp] = useState(0)
|
const [otp, setOtp] = useState(0)
|
||||||
|
|
||||||
|
async function registerNotification() {
|
||||||
|
const permission = await requestPermission()
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
registerNotification()
|
||||||
|
}, [])
|
||||||
|
|
||||||
const { token, isLoading } = useAuthSession()
|
const { token, isLoading } = useAuthSession()
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
return <Text>Loading...</Text>;
|
return <Text>Loading...</Text>;
|
||||||
|
|||||||
13
lib/api.ts
13
lib/api.ts
@@ -2,7 +2,8 @@ import axios from 'axios';
|
|||||||
|
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
// baseURL: 'http://10.0.2.2:3000/api',
|
// 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 }) => {
|
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 })
|
const response = await api.delete(`/mobile/document/more`, { data })
|
||||||
return response.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 {
|
try {
|
||||||
const granted = await PermissionsAndroid.request(
|
const cek = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
|
||||||
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
if (!cek) {
|
||||||
);
|
const granted = await PermissionsAndroid.request(
|
||||||
return granted;
|
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
|
||||||
|
);
|
||||||
|
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Error requesting notification permissions:', err);
|
console.warn('Error requesting notification permissions:', err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getToken = async () => {
|
export const getToken = async () => {
|
||||||
try {
|
try {
|
||||||
const mess = await initializeFirebase();
|
const mess = await initializeFirebase();
|
||||||
const token = await mess?.getToken();
|
const token = await mess?.getToken();
|
||||||
console.log('Token:', token);
|
|
||||||
return token;
|
return token;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error getting token:", error);
|
console.error("Error getting token:", error);
|
||||||
@@ -59,7 +65,7 @@ export const useNotification = () => {
|
|||||||
const initializeAndSetup = async () => {
|
const initializeAndSetup = async () => {
|
||||||
try {
|
try {
|
||||||
// await initializeFirebase();
|
// await initializeFirebase();
|
||||||
await requestPermission();
|
// await requestPermission();
|
||||||
await getToken();
|
await getToken();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to setup notifications:', 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 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 CryptoES from "crypto-es";
|
||||||
|
import { router } from "expo-router";
|
||||||
|
import { createContext, MutableRefObject, ReactNode, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
const AuthContext = createContext<{
|
const AuthContext = createContext<{
|
||||||
signIn: (arg0: string) => void;
|
signIn: (arg0: string) => void;
|
||||||
@@ -24,12 +26,12 @@ export function useAuthSession() {
|
|||||||
return useContext(AuthContext);
|
return useContext(AuthContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function AuthProvider ({children}:{children: ReactNode}): ReactNode {
|
export default function AuthProvider({ children }: { children: ReactNode }): ReactNode {
|
||||||
const tokenRef = useRef<string|null>(null);
|
const tokenRef = useRef<string | null>(null);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async ():Promise<void> => {
|
(async (): Promise<void> => {
|
||||||
const token = await AsyncStorage.getItem('@token');
|
const token = await AsyncStorage.getItem('@token');
|
||||||
tokenRef.current = token || '';
|
tokenRef.current = token || '';
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
@@ -49,15 +51,39 @@ export default function AuthProvider ({children}:{children: ReactNode}): ReactN
|
|||||||
})
|
})
|
||||||
|
|
||||||
const signIn = useCallback(async (token: string) => {
|
const signIn = useCallback(async (token: string) => {
|
||||||
await AsyncStorage.setItem('@token', token);
|
const hasil = await decryptToken(String(token))
|
||||||
tokenRef.current = token;
|
const permission = await requestPermission()
|
||||||
router.replace('/home')
|
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 () => {
|
const signOut = useCallback(async () => {
|
||||||
await AsyncStorage.setItem('@token', '');
|
try {
|
||||||
tokenRef.current = null;
|
const hasil = await decryptToken(String(tokenRef.current))
|
||||||
router.replace('/');
|
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 (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user