Login System Tutorial Part 7 - LogoutΒΆ

In this tutorial, we will talk about logging out a user account from the home page.

First of all, go to home.html in the views folder and add logout button below the user login status.

<body>
    <h1>Login as {{.User}}
    {{if eq .OTP true}} with {{else}} without {{end}}
    2FA Authentication</h1>

    <!-- ADD THIS PIECE OF CODE TO CREATE LOGOUT BUTTON -->
    <form method="POST">
        <button type="submit" name="request" value="logout">Logout</button>
    </form>
</body>

Go to login.html and add this piece of code to notify the user that he logged out his account.

<body>
    <!-- ADD THIS PIECE OF CODE TO CREATE LOGOUT NOTIFICATION -->
    <p>{{.Message}}</p>

    <form method="POST">
        <!-- Some input fields -->
    </form>
</body>

Now go to login.go in handlers folder and apply the following codes below to delete the cookie when the user logged out his account:

if r.FormValue("request") == "login" {
    // Some codes
}

// Check if the request submitted is logout
if r.FormValue("request") == "logout" {
    // Assign the message to the Message field of userContext
    userContext.Message = "User has logged out."

    // Logout the user in uAdmin
    uadmin.Logout(r)

    // Deletes the cookie
    usersession := &http.Cookie{
        Name:   "user_session",
        Value:  "",
        MaxAge: -1,
    }
    http.SetCookie(w, usersession)

    // Pass the userContext data object to the HTML file
    uadmin.HTMLContext(w, userContext, "views/login.html")
    return
}

Run your application. Go to the login path in the address bar (e.g. http://0.0.0.0:8080/login/). Assign the username and password in the login form (e.g. admin, admin). Click Login button to submit.

../../_images/adminusernamepassword.png

As expected, the logout button has been created in the form. Click Logout button and see what happens.

../../_images/logoutbuttoncreated.png

Result

../../_images/logoutnotification.png

Now check the user_session cookie to ensure that it was deleted.

../../_images/usersessioncookiedeleted.png

In the next part, we will discuss about reading a cookie and getting the user from the model based on the value of the cookie to ensure that the user is active.

Click here to view the full source code in this part.