Commit 3c8c5319 by Torkel Ödegaard

Sign up and login work

parent 4786e0f8
Subproject commit 3bf7f6ed5fa348d7983a4ae3bf17d280d5a5ed13
Subproject commit 8ed921240133c8638ecd0b2973bae36cdb5f6ccb
......@@ -19,7 +19,7 @@ func Register(r *macaron.Macaron) {
// not logged in views
r.Get("/", reqSignedIn, Index)
r.Post("/logout", LogoutPost)
r.Post("/login", LoginPost)
r.Post("/login", bind(dtos.LoginCommand{}), LoginPost)
r.Get("/login/:name", OAuthLogin)
r.Get("/login", Index)
......@@ -35,7 +35,7 @@ func Register(r *macaron.Macaron) {
// sign up
r.Get("/signup", Index)
r.Post("/api/account/signup", SignUp)
r.Put("/api/user/signup", bind(m.CreateUserCommand{}), SignUp)
// authed api
r.Group("/api", func() {
......
......@@ -8,9 +8,10 @@ import (
m "github.com/torkelo/grafana-pro/pkg/models"
)
type LoginResult struct {
Status string `json:"status"`
User CurrentUser `json:"user"`
type LoginCommand struct {
User string `json:"user" binding:"Required"`
Password string `json:"password" binding:"Required"`
Remember bool `json:"remember"`
}
type CurrentUser struct {
......
......@@ -9,21 +9,9 @@ import (
"github.com/torkelo/grafana-pro/pkg/util"
)
type loginJsonModel struct {
Email string `json:"email" binding:"required"`
Password string `json:"password" binding:"required"`
Remember bool `json:"remember"`
}
func LoginPost(c *middleware.Context) {
var loginModel loginJsonModel
if !c.JsonBody(&loginModel) {
c.JSON(400, util.DynMap{"message": "bad request"})
return
}
func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) {
userQuery := m.GetUserByLoginQuery{LoginOrEmail: loginModel.Email}
userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.User}
err := bus.Dispatch(&userQuery)
if err != nil {
......@@ -33,7 +21,7 @@ func LoginPost(c *middleware.Context) {
user := userQuery.Result
passwordHashed := util.EncodePassword(loginModel.Password, user.Salt)
passwordHashed := util.EncodePassword(cmd.Password, user.Salt)
if passwordHashed != user.Password {
c.JsonApiErr(401, "Invalid username or password", err)
return
......@@ -41,11 +29,7 @@ func LoginPost(c *middleware.Context) {
loginUserWithUser(user, c)
var resp = &dtos.LoginResult{}
resp.Status = "Logged in"
resp.User.Login = user.Login
c.JSON(200, resp)
c.JsonOK("User logged in")
}
func loginUserWithUser(user *m.User, c *middleware.Context) {
......
......@@ -8,13 +8,7 @@ import (
)
// POST /api/account/signup
func SignUp(c *middleware.Context) {
var cmd m.CreateUserCommand
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation error", nil)
return
}
func SignUp(c *middleware.Context, cmd m.CreateUserCommand) {
cmd.Login = cmd.Email
cmd.Salt = util.GetRandomString(10)
......@@ -25,5 +19,9 @@ func SignUp(c *middleware.Context) {
return
}
c.JsonOK("User created")
user := cmd.Result
loginUserWithUser(&user, c)
c.JsonOK("User created and logged in")
}
......@@ -30,13 +30,13 @@ type User struct {
// COMMANDS
type CreateUserCommand struct {
Email string
Login string
Name string
Company string
Password string
Salt string
IsAdmin bool
Email string `json:"email" binding:"Required"`
Login string `json:"login"`
Name string `json:"name"`
Company string `json:"compay"`
Password string `json:"password" binding:"Required"`
Salt string `json:"-"`
IsAdmin bool `json:"-"`
Result User `json:"-"`
}
......
......@@ -20,8 +20,6 @@ func TestMigrations(t *testing.T) {
testDBs := []sqlutil.TestDB{
sqlutil.TestDB_Sqlite3,
// sqlutil.TestDB_Mysql,
// sqlutil.TestDB_Postgres,
}
for _, testDB := range testDBs {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment