Help us shape the future of Go! Take the 2026 Go Developer Survey

golang-auth-sessions

command module
v0.0.0-...-b0fa743 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2018 License: MIT Imports: 7 Imported by: 0

README

Golang authentication and sessions

A custom registration, authentication and session handling implemented in Golang and MySQL.

Motivation

This project was started purely as a learning tool to aid myself with learning Golang and specifically web development with Golang. I wanted to incorporate Go with a RDBMS such as MySQL as well as implement other features such as account verification upon sign up and sessions without the use of a framework.

Screenshots

activation-reminder

login

profile

register

Tech/packages used

Features

  • Registration and activation
    • Activation link is sent to the user's registration email with a UUID token embedded upon signing up. This one time use link must be used before it is expired or it will not activate the associated account.
    • Activation links are one time use. Once clicked, it will be marked as used in the database and cannot be used again.
    • Activaton links can be resent by visiting the /send-activation route which will generate a new activation link with a new expiry date.
  • Authentication
    • Registered users with activated accounts can login with their email and password.
    • The email address the user enters is checked against the database. If the email exists, the password entered is hashed and compared against the hashed password entry in the database and if the hash matches, the user is logged in and a session is created.
  • Sessions
    • Once a user successfully authenticates, a session is created.
    • The UUID generated upon logging in is inserted in the sessions table with the user's unique id.
    • A cookie is sent to the client with the same UUID that was inserted in the database.
    • When a user makes a request for a secured route e.g. /my-profile, the session cookie is received by the server and verified against the sessions table. If the same UUID in the cookie is also present in the sessions table, the user is granted access to the secured route.
    • Sessions and cookies have a maximum life before they expire, once expired, the user will need to login again.
    • Sessions and cookies are automatically renewed when the user makes a request to any secured routes. This will reset the max life of the session and cookie.
    • Session entries along with the session cookie is destroyed upon logging out.
  • Input validation
    • The registration, login and any routes with input forms are validated to make sure they are not empty, have the correct format or unique if a user is entering their email address on sign up.

Setup

  • Load the MySQL database schema located at ./static/db/userAuthDBSchema.sql
  • Set the app environment variables located at ./.env
    • DB_USERNAME = Your MySQL server username
    • DB_PASSWORD = Your MySQL server password
    • DB_ADDRESS = Your MySQL server address
    • DB_PORT = Your MySQL server port
    • DB_NAME = The database name, the default is "userAuthDB" as set on the schema.
    • SENDER_ADDRESS = Verification link sender email address
    • SENDER_PASSWORD = Verification link sender email address password
    • SMTP_SERVER = Verification link sender email address SMTP server
  • Build the binary by running go build and then run the executable
go build

  • Visit localhost:8080 on your web browser

Credits

Thanks to Todd McLeod and particularly his course on Udemy(Course Repo).

License

MIT License

Copyright (c) 2018 Marck Muñoz

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
config
db
Package db connects to the database
Package db connects to the database
globals
Package globals stores the global variables
Package globals stores the global variables
smtp
Package smtp is used to set up the activation email sender email
Package smtp is used to set up the activation email sender email
Package handlers is a list of handler functions.
Package handlers is a list of handler functions.
models
sessions
Package sessions is for session related functions
Package sessions is for session related functions
tokens
Package tokens is used for activation token related function
Package tokens is used for activation token related function
users
Package users is used for user related functions
Package users is used for user related functions
validators
Package validators is used to validate forms
Package validators is used to validate forms

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL