How to use the @aws-amplify/auth.currentSession function in @aws-amplify/auth

To help you get started, we’ve selected a few @aws-amplify/auth examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TreeHacks / root / src / index.tsx View on Github external
export const custom_header = async () => { 
    try {
        return { Authorization: (await Auth.currentSession()).getIdToken().getJwtToken() }
    }
    catch (e) {
        console.warn(e, "Defaulting to stored JWT in localStorage...");
        // Get JWT from SAML.
        return { Authorization: await asyncLocalStorage.getItem("jwt") } 
    }
}
Auth.configure({
github TreeHacks / root / src / store / auth / actions.ts View on Github external
}
    else {
      Auth.signOut();
      let attributes: IUserAttributes = { "name": parsed["name"], "email": parsed["email"], "email_verified": parsed["email_verified"], "cognito:groups": parsed["cognito:groups"] };
      return await {
        "username": parsed["sub"],
        attributes
      };
    }
  }

  // If JWT from SAML has expired, or if there is no JWT in the first place, run this code.
  // Need to parse our local JWT as well to get cognito:groups attribute, because Auth.currentAuthenticatedUser() does not return user groups.
  return Promise.all([
    Auth.currentAuthenticatedUser(),
    parseJwt((await Auth.currentSession()).getIdToken().getJwtToken())
  ]).then(([user, token]) => {
    user.attributes["cognito:groups"] = token["cognito:groups"];
    return user;
  });
}
github awslabs / aws-full-stack-template / assets / src / App.tsx View on Github external
async componentDidMount() {
    try {
      if (await Auth.currentSession()) {
        this.userHasAuthenticated(true);
      }
    }
    catch (e) {
      if (e !== 'No current user') {
        alert(e);
      }
    }

    this.setState({ isAuthenticating: false });
  }
github aws-amplify / amplify-js / packages / api / src / API.ts View on Github external
if (!credentialsOK) {
					throw new Error('No credentials');
				}
				break;
			case 'OPENID_CONNECT':
				const federatedInfo = await Cache.getItem('federatedInfo');

				if (!federatedInfo || !federatedInfo.token) {
					throw new Error('No federated jwt');
				}
				headers = {
					Authorization: federatedInfo.token,
				};
				break;
			case 'AMAZON_COGNITO_USER_POOLS':
				const session = await Auth.currentSession();
				headers = {
					Authorization: session.getAccessToken().getJwtToken(),
				};
				break;
			default:
				headers = {
					Authorization: null,
				};
				break;
		}

		return headers;
	}
github systemaccounting / mxfactorial / react / src / hooks / useNotifications.js View on Github external
const getAuthToken = async () => {
  try {
    const session = await Auth.currentSession()
    return session.getIdToken().getJwtToken()
  } catch (e) {
    return ''
  }
}
github yhenni1989 / Plush / App.js View on Github external
jwtToken: async () => (
      await Auth.currentSession()).getAccessToken().getJwtToken(),
  },
github aws-amplify / amplify-js / packages / api-graphql / src / GraphQLAPI.ts View on Github external
if (!credentialsOK) {
					throw new Error('No credentials');
				}
				break;
			case 'OPENID_CONNECT':
				const federatedInfo = await Cache.getItem('federatedInfo');

				if (!federatedInfo || !federatedInfo.token) {
					throw new Error('No federated jwt');
				}
				headers = {
					Authorization: federatedInfo.token,
				};
				break;
			case 'AMAZON_COGNITO_USER_POOLS':
				const session = await Auth.currentSession();
				headers = {
					Authorization: session.getAccessToken().getJwtToken(),
				};
				break;
			default:
				headers = {
					Authorization: null,
				};
				break;
		}

		return headers;
	}
github webiny / webiny-js / packages / app-plugin-security-cognito / src / index.js View on Github external
const getIdToken = async () => {
                const cognitoUser = await Auth.currentSession();
                return cognitoUser ? cognitoUser.idToken.jwtToken : null;
            };