User management library written in Go
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.
- Helps you register new users.
- Validates credentials on login.
- Allows changing passwords.
- Allows recovering forgotten passwords.
- Create sessions
- Does not verify or send email.
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
}
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")
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
}
// 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
}
// 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
}
username, err := personna.CurrentUser(r) // r is the request pointer
if err != nil {
// No user is logged in
}
// 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
}