-
Notifications
You must be signed in to change notification settings - Fork 1
Getting started
This article about how to use kidstuff/auth with your web server.
To use kidstuff/auth package with your application, you need to use an "manager" (a driver/backend implement of kidstuff/authmodel interface). In this article we will use an MongoDB manager.
First, run this command in your termial to get it.
go get github.com/kidstuff/auth-mongo-mngr
Let start a new project with two file.
- main.go: config the packge and routing.
- mail.go: implement for sending mail, kidstuff/auth provide an Notificator interface. We have an example of Notificator that use the Amazon Simple Email Service here.
- tools/setup.go: wee need a tool to prepare database (for each manager), add the default user...
####tools/setup.go
package main
import (
"fmt"
"github.com/kidstuff/auth-mongo-mngr"
"github.com/kidstuff/auth/authtool"
"labix.org/v2/mgo"
"os"
)
func main() {
MONGODB_URL := os.Getenv("MONGODB_URL")
SERVER_URL := os.Getenv("SERVER_URL")
DB_NAME := os.Getenv("DB_NAME")
session, err := mgo.Dial(MONGODB_URL)
if err != nil {
fmt.Println(err)
return
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
db := session.DB(DB_NAME)
// The auth-mongo-mngr require to run Setup function
err = mgoauth.SetUp(db)
if err != nil {
fmt.Println(err);return
}
su := authtool.NewSetUp(mgoauth.NewMgoConfigMngr(db), mgoauth.NewMgoManager(db))
fmt.Println("Set default seetings...")
// the system require these settings, read wiki to understand them.
settings := map[string]string{
"auth_full_path": "http://localhost:8080/auth",
"auth_activate_redirect": "http://localhost:8081/#!/welcome",
"auth_approve_new_user": "false",
"auth_email_from": "foo@example.com",
"auth_send_activate_email": "true",
"auth_activate_email_subject": "Active your account",
"auth_activate_email_message": "Hi!\nPlease active your account by cliking here:\n%s",
"auth_send_welcome_email": "true",
"auth_welcome_email_subject": "Welcome!",
"auth_welcome_email_message": "Hi!\nWelcome you to join our community :)",
"auth_reset_redirect": "http://localhost:8081/#!/resetpassword/%s/%s",
"auth_reset_email_subject": "Password reset request",
"auth_reset_email_message": "Hi!\nTo reset your password please click the link bellow:\n%s",
}
err = su.SetSettings(settings)
if err != nil {
fmt.Println(err);return
}
fmt.Println("Default settings setted!")
fmt.Println("Add admin user...")
var email, pwd string
fmt.Println("Enter admin user Email:")
fmt.Scanf("%s", &email)
fmt.Println("Enter admin user Password:")
fmt.Scanf("%s", &pwd)
_, err = su.AddAdmin(email, pwd)
if err != nil {
fmt.Println(err)
}
}
Go a head by go run setup.go
####main.go
import (
"github.com/gorilla/mux"
"github.com/kidstuff/auth"
"github.com/kidstuff/auth-mongo-mngr"
"labix.org/v2/mgo"
"os"
)
func main() {
MONGODB_URL := os.Getenv("MONGODB_URL")
SERVER_URL := os.Getenv("SERVER_URL")
DB_NAME := os.Getenv("DB_NAME")
SES_SERVER := os.Getenv("SES_SERVER")
SES_USER := os.Getenv("SES_USER")
SES_PWD:= os.Getenv("SES_PWD")
session, err := mgo.Dial(MONGODB_URL)
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
db := session.DB(DB_NAME)
// NewSESNotificator return an auth.Notificator
auth.DEFAULT_NOTIFICATOR = NewSESNotificator(465, SES_SERVER, SES_USER, SES_PWD)
// And about Logger interface http://godoc.org/github.com/kidstuff/auth#Logger
auth.DEFAULT_LOGGER, _ = auth.NewSysLogger("kidstuff/auth")
// Initial is a function from kidstuff/auth-mongo-mngr driver,
// assign database will use with the auth system.
// Its also tell the main kidstuff/auth package to use it at the "manager",
// mostly, after this line, the developer don't need to import the mananger in their code.
// The name "Initial" is specific by manager packge, so remember to check it docuemnt.
mgoauth.Initial(db)
// We use gorilla/mux, you don't need to use it with the other part of you app.
// But some kidstuff/auth convenient function require a specific structrue of the handle
// routed with gorilla/mux. We will talk about it later.
r := mux.NewRouter()
// auth.Serve will panic if the manager doesn't config the package right.
// Now we have the auth REST API run at example.com/auth/xxx.
// To know what is the xxx part, read http://kidstuff.github.io/swagger/#!/default
auth.Serve(r.PathPrefix("/auth").Subrouter())
http.ListenAndServe(SERVER_URL, r)
}
Now we have a simple REST API, we have a swagger document at http://kidstuff.github.io/swagger/#!/default
Next you can intergrate your application by our In-Go API.
After all, this is an live example that you can clone and run https://github.com/kidstuff/auth-example
This article written in "lazy mode", so if you have any question/suggestion, please reply here https://groups.google.com/forum/#!category-topic/kidstuff-opensources/auth-main-package/TdqCQ6r4a6g