Clear the Session on Logout

From @jayair on Mon Apr 10 2017 01:04:07 GMT+0000 (UTC)

Link to chapter -

Copied from original issue:

I kept logging out after a refresh. I have checked the code twice and event copied it from your github repository, but still kept logging out.

Any ideas what to do/check?

Hmmm we ensure that we load the session when the page loads back in this chapter -

So it is the componentDidMount method in the App.js that you should check.

Jep I have!

import React, { Component, Fragment } from "react";
import { Auth } from "aws-amplify";
import { Link, withRouter } from "react-router-dom";
import { Nav, Navbar, NavItem } from "react-bootstrap";
import { LinkContainer } from "react-router-bootstrap";
import Routes from "./Routes";
import "./App.css";

class App extends Component {
  constructor(props) {

    this.state = {
      isAuthenticated: false,
      isAuthenticating: true

  async componentDidMount() {
    try {
      if (await Auth.currentSession()) {
    catch(e) {
      if (e !== 'No current user') {

    this.setState({ isAuthenticating: false });

  userHasAuthenticated = authenticated => {
    this.setState({ isAuthenticated: authenticated });

  handleLogout = async event => {
    await Auth.signOut();



  render() {
    const childProps = {
      isAuthenticated: this.state.isAuthenticated,
      userHasAuthenticated: this.userHasAuthenticated
    return (
      !this.state.isAuthenticating &&
      <div className="App container">
        <Navbar fluid collapseOnSelect>
              <Link to="/">Scratch</Link>
            <Navbar.Toggle />
            <Nav pullRight>
                ? <Fragment>
                    <LinkContainer to="/settings">
                    <NavItem onClick={this.handleLogout}>Logout</NavItem>
                : <Fragment>
                    <LinkContainer to="/signup">
                    <LinkContainer to="/login">
        <Routes childProps={childProps} />

export default withRouter(App);

The other area to check is your Routes.js and make sure it is using the childProps and AppliedRoute -

It’s the same like the instruction. Like I said, I even copied the file from your github repository. I expect it’s has something to do with Amplify.

I also see a error message when I do an API call:

cannot get guest credentials when mandatory signin enabled

In that case, it is worth checking the settings for your Identity Pool. Maybe you are allowing access for unauthenticated users?

I see this error in the console: API - ensure credentials error: “cannot get guest credentials when mandatory signin enabled”

If you go to your Identity Pool > Click Edit identity pool > Expand Unauthenticated identities. Does it look like this?

Yes it does :frowning:
I created a new pool, but the same.

You mentioned the APIs, did you test them successfully by following this chapter?

I have it working with the AWS mobile hub.

I saw that I had an older amplify version, but couldn’t upgrade. So maybe it had something to do with that.

Thanks for your help!

Hello. I have the same issue. I think this issue should be escalated.
Basically I followed the guide step by step and when I reached the Create Note page chapter, the response to clicking the Create Button is an alert box saying “Error: Network Error”, in console it says:

“Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).”.

So I add the header in the API.POST method and I get the same alert box but in console it says:

“Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).”

Just to add. The IAM Policy simulator responds an “allowed”. Also when running API mehtods in cli using invoke I had no issue.

You might need to debug this with some logging. It is most likely related to an error while running the Lambda function. The error message is a little misleading at times.

Enable logging for Lambda and API Gateway and post the logs here.

Found the answer here

1 Like