I have a Next.JS application (SSR'd react) and i'd like to check whether there the user is logged in or not. I've got Apollo client hooked up and working with Coginto User Pools but for some reason this code here.. (which is run on the server) does not return a user. (When i'm definitely logged in)
import { Auth } from 'aws-amplify'
export default () =>
Auth.currentAuthenticatedUser()
.then(user => ({
loggedInUser: user,
}))
.catch(() => ({
loggedInUser: {},
}))
And i'm calling the function by running:
const { loggedInUser } = await checkLoggedIn()
Is there a better way to check if a user is logged in on the server?
Versions:
aws-amplify: 0.4.2-unstable.21
aws-amplify-react: 0.1.48-unstable.21
@lolcoolkat For now that function is not supported in the server side. You need to store that user by yourself. Also we can expose an interface for you to pass a storage object to the library. Mark this as enhancement
@powerful23 Could you please elaborate more about what you mean with storing the user by yourself? As I understand, Amplify stores the user tokens on browser in localStorage. If the user logs in on the client site, how can you pass those tokens back to the server to check if user is logged in?
@ngocketit What I mean is that Amplify has the option to setup your own storage object to store tokens: https://aws-amplify.github.io/docs/js/authentication#managing-security-tokens . In case you don't want to use localStorage or its unavailable (i.e. in server side),
@powerful23 Thanks for replying! However, I'm thinking and accepting the fact that maybe it's not possible to do authentication with Amplify on the server while keep it in sync with client. User logs in on client but currently there is no way to keep that same user on the server (e.g, the same access token that can be used to send the request on the server). We can have a different storage on the server but how to keep it in sync with localStorage on client. If the client can send the token in forms of cookies then we may have some hope, otherwise I think it's not possible.
Any update on this?
Any update?
Most helpful comment
Any update on this?