stopped at access token
This commit is contained in:
@@ -8,13 +8,14 @@ import { useFocusEffect, useNavigation } from '@react-navigation/native';
|
||||
import * as ImagePicker from 'expo-image-picker';
|
||||
import ScannedDataBox from '../components/ScannedDataBox';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { RootState, AppDispatch } from '../store';
|
||||
import { addQRCode } from '../reducers/qrCodesReducer'; // Assuming you have actions defined for Redux
|
||||
import { detectQRCodeType, verifyURL, checkRedirects } from '../api/qrCodeAPI'; // Import utility functions
|
||||
|
||||
// Main Function
|
||||
const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanData }) => {
|
||||
const navigation = useNavigation(); // call Navigation bar
|
||||
const dispatch = useDispatch(); // Use dispatch for Redux actions
|
||||
const dispatch = useDispatch<AppDispatch>(); // Use dispatch for Redux actions
|
||||
|
||||
const [showSplash, setShowSplash] = useState<boolean>(true); // call splash screen
|
||||
const qrCodeContext = useContext(QRCodeContext); // From ./types.ts
|
||||
@@ -147,15 +148,6 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
||||
}
|
||||
};
|
||||
|
||||
// // Clear scan data when screen is focused
|
||||
// useEffect(() => {
|
||||
// const unsubscribe = navigation.addListener('focus', () => {
|
||||
// clearScanDataInternal();
|
||||
// console.log("Screen focused, scan data cleared");
|
||||
// });
|
||||
// return unsubscribe;
|
||||
// }, [navigation]);
|
||||
|
||||
// Clear scan data when screen is focused
|
||||
useFocusEffect(
|
||||
useCallback(() => {
|
||||
@@ -206,9 +198,9 @@ const QRScannerScreen: React.FC<{ clearScanData: () => void }> = ({ clearScanDat
|
||||
<TouchableOpacity onPress={toggleTorch} style={styles.flashButton}>
|
||||
<Ionicons name="flashlight" size={24} color="#fff" />
|
||||
</TouchableOpacity>
|
||||
<TouchableOpacity onPress={handleTestScan} style={styles.testButton}>
|
||||
{/* <TouchableOpacity onPress={handleTestScan} style={styles.testButton}>
|
||||
<Ionicons name="bug" size={24} color="#fff" />
|
||||
</TouchableOpacity>
|
||||
</TouchableOpacity> */}
|
||||
<TouchableOpacity onPress={readQRFromImage} style={styles.galleryButton}>
|
||||
<Ionicons name="image" size={24} color="#fff" />
|
||||
</TouchableOpacity>
|
||||
@@ -252,6 +244,8 @@ const styles = StyleSheet.create({
|
||||
justifyContent: 'center',
|
||||
alignItems: 'center',
|
||||
backgroundColor: '#f8f0fc',
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
},
|
||||
cameraContainer: {
|
||||
height: '60%',
|
||||
|
||||
@@ -1,15 +1,102 @@
|
||||
import { View, Text, StyleSheet, TouchableOpacity, Linking, Button } from 'react-native';
|
||||
import { useAuthenticator } from '@aws-amplify/ui-react-native';
|
||||
import useFetchUserAttributes from '../hooks/useFetchUserAttributes';
|
||||
import { fetchAuthSession, getCurrentUser, signInWithRedirect } from 'aws-amplify/auth';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { Buffer } from 'buffer';
|
||||
|
||||
|
||||
function SignOutButton() {
|
||||
const { signOut } = useAuthenticator();
|
||||
return <Button title="Sign Out" onPress={signOut} />;
|
||||
}
|
||||
const handleSignInWithRedirect = async () => {
|
||||
try {
|
||||
await signInWithRedirect();
|
||||
} catch (error) {
|
||||
console.error('Error during sign in:', error);
|
||||
}
|
||||
};
|
||||
|
||||
const SettingsScreen: React.FC = () => {
|
||||
const { userAttributes } = useFetchUserAttributes();
|
||||
const [googleAccessToken, setGoogleAccessToken] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const getGoogleAccessToken = async () => {
|
||||
try {
|
||||
const currentUser = await getCurrentUser();
|
||||
console.log('Current user:', currentUser);
|
||||
|
||||
const { tokens } = await fetchAuthSession();
|
||||
const test = await fetchAuthSession();
|
||||
console.log('Tokens:', tokens);
|
||||
console.log("aws access token: ", tokens.accessToken.toString());
|
||||
console.log("test ", test);
|
||||
|
||||
if (tokens?.idToken) {
|
||||
const idToken = tokens.idToken.toString();
|
||||
console.log('ID Token:', idToken);
|
||||
|
||||
const parts = idToken.split('.');
|
||||
if (parts.length !== 3) {
|
||||
throw new Error('ID token is not a valid JWT');
|
||||
|
||||
}
|
||||
|
||||
const payload = parts[1];
|
||||
const decodedPayload = Buffer.from(payload, 'base64').toString('utf8');
|
||||
console.log('Decoded payload:', decodedPayload);
|
||||
|
||||
let parsedPayload;
|
||||
try {
|
||||
parsedPayload = JSON.parse(decodedPayload);
|
||||
} catch (parseError) {
|
||||
console.error('Error parsing payload:', parseError);
|
||||
console.error(`Parse error: ${parseError.message}\nPayload: ${decodedPayload}`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Parsed payload:', parsedPayload);
|
||||
// Options for toLocaleString
|
||||
const options: Intl.DateTimeFormatOptions = {
|
||||
timeZone: 'Asia/Singapore', // UTC+8 timezone
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
second: '2-digit'
|
||||
};
|
||||
|
||||
|
||||
if (parsedPayload["custom:access_token"]) {
|
||||
console.log('Google Access Token:', parsedPayload["custom:access_token"]);
|
||||
console.log('Google Refresh Token: ', parsedPayload["custom:refresh_token"]);
|
||||
|
||||
setGoogleAccessToken(parsedPayload["custom:access_token"]);
|
||||
console.log("auth_time: ", new Date(parsedPayload.auth_time * 1000).toLocaleString('en-US', options));
|
||||
console.log("iat: ", new Date(parsedPayload.iat * 1000).toLocaleString('en-US', options));
|
||||
console.log("expiry: ", new Date(parsedPayload.exp * 1000).toLocaleString('en-US', options));
|
||||
console.log("date created: ", new Date(1721715837500).toLocaleString('en-US', options));
|
||||
|
||||
} else {
|
||||
console.error('No Google access token found in the payload');
|
||||
}
|
||||
|
||||
} else {
|
||||
console.error('No ID token found in the session');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting Google access token:', error);
|
||||
}
|
||||
};
|
||||
|
||||
if (userAttributes) {
|
||||
getGoogleAccessToken();
|
||||
}
|
||||
}, [userAttributes]);
|
||||
|
||||
const handleLinkPress = (url: string) => {
|
||||
Linking.openURL(url);
|
||||
};
|
||||
@@ -22,10 +109,13 @@ const SettingsScreen: React.FC = () => {
|
||||
{userAttributes ? (
|
||||
<View>
|
||||
<Text style={styles.userName}>Hello, {userAttributes?.name}</Text>
|
||||
{googleAccessToken && (
|
||||
<Text>Google Access Token: {googleAccessToken.substring(0, 10)}...</Text>
|
||||
)}
|
||||
<SignOutButton />
|
||||
</View>
|
||||
) : (
|
||||
<TouchableOpacity style={styles.loginButton}>
|
||||
<TouchableOpacity style={styles.loginButton} onPress={handleSignInWithRedirect}>
|
||||
<Text style={styles.loginButtonText}>Log In</Text>
|
||||
</TouchableOpacity>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user