persona

package module
v0.0.0-...-54ee07f Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2021 License: MIT Imports: 11 Imported by: 0

README

persona

GoDoc GoReport License GoLang Github Actions

User management library written in Go

What is Persona?

Persona is a simple, functional service to let you create, verify and update user profiles.

Persona is not for everyone; if your login system is too complex and relies on many factors, Persona is not for you. However, persona works great for most use cases.

What does it do?

  1. Helps you register new users.
  2. Validates credentials on login.
  3. Allows changing passwords.
  4. Allows recovering forgotten passwords.
  5. Create sessions

What does it NOT do?

  1. Does not verify or send email.

How to use it

go get -u gitote.in/dimensi0n/persona

Use this model template and add what you want

type User struct {
	persona.User
}

this is equivalent to :

type User struct {
	gorm.Model        // REQUIRED
	Username   string // REQUIRED
	Password   string // REQUIRED
	Mail       string `gorm:"not null;unique"` // REQUIRED
	Loggedin   bool   `gorm:"default:true"`    // REQUIRED
}
Config

You'll need to configure Persona. This is an example

db, err := gorm.Open("sqlite3", "gorm.db")
if err != nil {
	// ERROR
}
defer db.Close()

db.AutoMigrate(&User{})

// If uid field is "username"
// If your users connect them with their username
persona.Config(db, "username")

// If uid field is "email"
// If your users connect them with their email
persona.Config(db, "email")
Signup
user := User{"Username", persona.HashPassword("Pasword"), "mail@mail.com"}
err := persona.Signup(&user, user.Username, w) // &user is the struct to save && w is the response writer
if err := nil {
    // There is an error while attempting to signup the user 
}
Login
// Username/Password
user := User{Username: "Username", Password: "Password"}
err := persona.Login(user.Username, user.Password, w) // &user is the struct to save username is the UID field && w is the response writer
if err := nil {
    // User credentials are false
}

// Email/Password
user := User{Email: "mail@mail.com", Password: "Password"}
err := persona.Login(user.Mail, user.Password, w) // email is the UID field && w is the response writer
if err := nil {
    // User credentials are false 
}
Logout
// Username/Password
user := User{Username: "Username"}
err := persona.Logout(user.Username, w) // w is the response writer
if err := nil {
    // There is an error while attempting to logout the user 
}

user := User{Mail: "mail@mail.com"}
err := persona.Logout(user.Mail, w) // w is the response writer
if err := nil {
    // There is an error while attempting to logout the user 
}
Get current user
username, err := personna.CurrentUser(r) // r is the request pointer
if err != nil {
	// No user is logged in
}
Recover password
// Username/Password
user := User{Username: "Username", Password: "Password"}
err := persona.RecoverPassword(user.Username, user.Password, "new password")
if err != nil {
    // There is an error while attemting to change user password
}

// Email/Password
user := User{Mail: "mail@mail.com", Password: "Password"}
err := persona.RecoverPassword(user.Mail, user.Password, "new password")
if err != nil {
    // There is an error while attemting to change user password
}

Documentation

Overview

Package persona provides user mangement fonctions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Config

func Config(db *gorm.DB, newUID string)

Config Persona

func GetCurrentUser

func GetCurrentUser(r *http.Request) (string, error)

GetCurrentUser returns current user username/email

func HashPassword

func HashPassword(password string) string

HashPassword hashes the password before saving it

func Login

func Login(uid string, password string, w http.ResponseWriter) error

Login logs in the user

func Logout

func Logout(uid string, w http.ResponseWriter) error

Logout logs out the user

func RecoverPassword

func RecoverPassword(uid string, oldPassword string, newPassword string) error

RecoverPassword recovers the user password

func Signup

func Signup(user interface{}, username string, w http.ResponseWriter) error

Signup register the user

Types

type Sessionemail

type Sessionemail struct {
	gorm.Model
	Mail  string `gorm:"not null;unique"`
	Token string
}

Sessionemail struct

type Sessionusername

type Sessionusername struct {
	gorm.Model
	Username string `gorm:"not null;"`
	Token    string
}

Sessionusername struct

type User

type User struct {
	gorm.Model        // REQUIRED
	Username   string `gorm:"not null;unique"` // REQUIRED
	Password   string // REQUIRED
	Mail       string `gorm:"not null;unique"` // REQUIRED
	Loggedin   bool   `gorm:"default:true"`    // REQUIRED
}

User default struct

Jump to

Keyboard shortcuts

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