Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the logged in user in the application #62

Open
nagendrakumar02 opened this issue Apr 5, 2023 · 3 comments
Open

How to get the logged in user in the application #62

nagendrakumar02 opened this issue Apr 5, 2023 · 3 comments

Comments

@nagendrakumar02
Copy link

How can we help?

I am currently using cognito@edge and attached it to the CDN. How can I retrieve the userId who has logged into the application?

@unitypark
Copy link

unitypark commented Apr 15, 2023

all retrieved information are saved in the cookie. one of them has a value of userId which has .LastAuthUser as suffix in key

@nagendrakumar02
Copy link
Author

nagendrakumar02 commented Apr 15, 2023 via email

@67726e
Copy link

67726e commented Jun 9, 2023

At a high-level, this is the flow of the code in the application at-present.

this._cookieBase = `CognitoIdentityServiceProvider.${params.userPoolAppId}`;

_buildCookieAccessTokenKey = (username: string) => `${this._cookieBase}.${username}.accessToken`;
_buildCookieIDTokenKey = (username: string) => `${this._cookieBase}.${username}.idToken`;
_buildCookieRefreshTokenKey = (username: string) => `${this._cookieBase}.${username}.refreshToken`;
_buildCookieScopeKey = (username: string) => `${this._cookieBase}.${username}.tokenScopesString`;
_buildCookieUsernameKey = () => `${this._cookieBase}.LastAuthUser`;

// Assumed that `cookieHeaders[string] => { key: string, value: string, };`
// Bearing in mind that the cookies may not actually exist :)
const cookieUsername = cookieHeaders[this._buildCookieUsernameKey()];
const cookieAccessToken = cookieHeaders[this._buildCookieAccessTokenKey(cookieUsername?.value)];
const cookieIDToken = cookieHeaders[this._buildCookieIDTokenKey(cookieUsername?.value)];
const cookieRefreshToken = cookieHeaders[this._buildCookieRefreshTokenKey(cookieUsername?.value)];

const tokens: Tokens = {
  accessToken: cookieAccessToken.value,
  idToken: cookieIDToken.value,
  refreshToken: cookieRefreshToken.value,
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants