Skip to content

An Open source user management library

License

Notifications You must be signed in to change notification settings

dimensi0n/persona

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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"), "[email protected]"}
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: "[email protected]", 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: "[email protected]"}
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: "[email protected]", Password: "Password"}
err := persona.RecoverPassword(user.Mail, user.Password, "new password")
if err != nil {
    // There is an error while attemting to change user password
}

About

An Open source user management library

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages