As the title suggests, what is the correct mental model for integrating NestJS/passport authentication and authorization with a React Native app?
For context, I'm not that familiar with JWT but trying to learn, and right now my react native/expo app authenticates users using AuthSession and google oauth api from Expo. Purely client-side. And Im just starting to work on a NestJS backend.
I really apologize that this is such a nebulous question, but where does NestJS/passport auth integrate with my previous auth solution? I feel like there's a massive gap in my knowledge.
What I would like to do, is create protected/authorized routes and associate documents in DB with users, etc.
Am I supposed to modify my backend auth routes to receive the accessToken from the clientside?
Here is my sign-in screen for react-native:
export default function SignInScreen() {
const { auth, setAuth } = useAuthStore()
const [_request, response, promptAsync] = useAuthRequest({
androidClientId: ANDROID_CLIENT_ID,
iosClientId: IOS_CLIENT_ID,
expoClientId: EXPO_CLIENT_ID,
})
useEffect(() => {
if (response?.type === 'success') {
console.log('response is', JSON.stringify(response, null, 2))
setAuth(response.authentication)
const persistAuth = async () => {
await AsyncStorage.setItem(
'auth',
JSON.stringify(response.authentication)
)
}
persistAuth()
}
}, [response])
/* NOTE:
* This is the code that fetches previous auth from AsyncStorage.
* Needs to be uncommented after styling sign in screen
*
*/
// useEffect(() => {
// const getPersistedAuth = async () => {
// const authString = await AsyncStorage.getItem('auth')
// if (authString) {
// const auth = JSON.parse(authString)
// if (!TokenResponse.isTokenFresh(auth.accessToken)) {
// return await refreshToken()
// }
// setAuth(auth);
// }
// }
// getPersistedAuth()
// }, [])
return (
<SafeAreaView style={{ flex: 1 }}>
<Box
flex={1}
justifyContent='center'
alignItems='center'
borderWidth={1}
backgroundColor="tertiary.300"
>
<Image
borderWidth={2}
borderColor='primary.50'
source={require('../../../assets/login-removebg-preview.png')}
size={'2xl'}
resizeMode='contain'
alt='sign in image'
/>
<Text
fontSize={34}
>Optimize Me</Text>
<AuthForm />
{!auth && (
<Button
variant={'ghost'}
colorScheme='emerald'
borderWidth={1.5}
borderColor='primary.50'
borderRadius={10}
padding={3}
leftIcon={<AntDesign name='google' size={24} color='#2BC8B2' />}
onPress={() => {
selectionAsync()
promptAsync()
}}
/>
)}
</Box>
<StatusBar style='auto' />
</SafeAreaView>
)
}
Should I wrap promptAsync()
and selectionAsync()
in a function that also hits the Auth endpoint at NestJS backend that would send over the accessToken, and then the server reissues a new one?
I would be super grateful if anyone could suggest alternative approaches as well. Any advice would be super appreciated.
Thanks in advance!