I've been trying to logout the signed-in user from my React native app (using Strapi). Should I redirect it to any specific URL or can I delete the JWT token? If yes, please point me in the right direction of how. Redirecting the "Logout" button to the Login page doesn't end the signed-in user's session for obvious reasons.
<Button
mode="outlined"
onPress={() => navigation.navigate('Login')}
style={styles.button2}
labelStyle={{
color: '#48b749',
fontSize: 12,
fontWeight: 'bold',
textAlignVertical: 'center',
textAlign: 'center',
}}>
Logout
</Button>
CodePudding user response:
you can store logged in user details in persisted state like async storage and when user will logout then delete this details. you can do something like this.
if(loggedInUser) { return <AuthenticatedStack /> } return <UnauthenticatedStack />
when you delete this loggedInUser from async storage it will redirect to Unauthenticated stack or login screen.
CodePudding user response:
Actually, you're already in the right direction. Strapi
doesn't have any api
as such for logging out a user as logout is only done locally on the client side. This only requires you to remove the jwt
& username
token from your browser localStorage
Let me provide you the quotes from their strapi blog posts below.
We've set up two endpoints:
- login: authenticates the user. On successful authentication, the JWT token will be available in the jwt property of the response object.
- user: retrieves the authenticated user's info. If the user is authenticated, the JWT token will be added to the request, allowing Strapi to identify the user. Since the response object is already the user info itself, we set propertyName to false.
We've also disabled the logout endpoint, since logging out a user is only done locally and doesn't require any request to Strapi's API. The token is simply removed from the local storage when the user logs out.
Speaking of the kind of code you'll have to write, you can refer the snippet below:
import { useHistory } from "react-router-dom";
let history = useHistory();
logout() {
localStorage.removeItem('jwt');
localStorage.removeItem('username');
history.push("/sigin");
}