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) { ...@@ -19,7 +19,7 @@ func Register(r *macaron.Macaron) {
// not logged in views // not logged in views
r.Get("/", reqSignedIn, Index) r.Get("/", reqSignedIn, Index)
r.Post("/logout", LogoutPost) r.Post("/logout", LogoutPost)
r.Post("/login", LoginPost) r.Post("/login", bind(dtos.LoginCommand{}), LoginPost)
r.Get("/login/:name", OAuthLogin) r.Get("/login/:name", OAuthLogin)
r.Get("/login", Index) r.Get("/login", Index)
...@@ -35,7 +35,7 @@ func Register(r *macaron.Macaron) { ...@@ -35,7 +35,7 @@ func Register(r *macaron.Macaron) {
// sign up // sign up
r.Get("/signup", Index) r.Get("/signup", Index)
r.Post("/api/account/signup", SignUp) r.Put("/api/user/signup", bind(m.CreateUserCommand{}), SignUp)
// authed api // authed api
r.Group("/api", func() { r.Group("/api", func() {
......
...@@ -8,9 +8,10 @@ import ( ...@@ -8,9 +8,10 @@ import (
m "github.com/torkelo/grafana-pro/pkg/models" m "github.com/torkelo/grafana-pro/pkg/models"
) )
type LoginResult struct { type LoginCommand struct {
Status string `json:"status"` User string `json:"user" binding:"Required"`
User CurrentUser `json:"user"` Password string `json:"password" binding:"Required"`
Remember bool `json:"remember"`
} }
type CurrentUser struct { type CurrentUser struct {
......
...@@ -9,21 +9,9 @@ import ( ...@@ -9,21 +9,9 @@ import (
"github.com/torkelo/grafana-pro/pkg/util" "github.com/torkelo/grafana-pro/pkg/util"
) )
type loginJsonModel struct { func LoginPost(c *middleware.Context, cmd dtos.LoginCommand) {
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
}
userQuery := m.GetUserByLoginQuery{LoginOrEmail: loginModel.Email} userQuery := m.GetUserByLoginQuery{LoginOrEmail: cmd.User}
err := bus.Dispatch(&userQuery) err := bus.Dispatch(&userQuery)
if err != nil { if err != nil {
...@@ -33,7 +21,7 @@ func LoginPost(c *middleware.Context) { ...@@ -33,7 +21,7 @@ func LoginPost(c *middleware.Context) {
user := userQuery.Result user := userQuery.Result
passwordHashed := util.EncodePassword(loginModel.Password, user.Salt) passwordHashed := util.EncodePassword(cmd.Password, user.Salt)
if passwordHashed != user.Password { if passwordHashed != user.Password {
c.JsonApiErr(401, "Invalid username or password", err) c.JsonApiErr(401, "Invalid username or password", err)
return return
...@@ -41,11 +29,7 @@ func LoginPost(c *middleware.Context) { ...@@ -41,11 +29,7 @@ func LoginPost(c *middleware.Context) {
loginUserWithUser(user, c) loginUserWithUser(user, c)
var resp = &dtos.LoginResult{} c.JsonOK("User logged in")
resp.Status = "Logged in"
resp.User.Login = user.Login
c.JSON(200, resp)
} }
func loginUserWithUser(user *m.User, c *middleware.Context) { func loginUserWithUser(user *m.User, c *middleware.Context) {
......
...@@ -8,13 +8,7 @@ import ( ...@@ -8,13 +8,7 @@ import (
) )
// POST /api/account/signup // POST /api/account/signup
func SignUp(c *middleware.Context) { func SignUp(c *middleware.Context, cmd m.CreateUserCommand) {
var cmd m.CreateUserCommand
if !c.JsonBody(&cmd) {
c.JsonApiErr(400, "Validation error", nil)
return
}
cmd.Login = cmd.Email cmd.Login = cmd.Email
cmd.Salt = util.GetRandomString(10) cmd.Salt = util.GetRandomString(10)
...@@ -25,5 +19,9 @@ func SignUp(c *middleware.Context) { ...@@ -25,5 +19,9 @@ func SignUp(c *middleware.Context) {
return 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 { ...@@ -30,13 +30,13 @@ type User struct {
// COMMANDS // COMMANDS
type CreateUserCommand struct { type CreateUserCommand struct {
Email string Email string `json:"email" binding:"Required"`
Login string Login string `json:"login"`
Name string Name string `json:"name"`
Company string Company string `json:"compay"`
Password string Password string `json:"password" binding:"Required"`
Salt string Salt string `json:"-"`
IsAdmin bool IsAdmin bool `json:"-"`
Result User `json:"-"` Result User `json:"-"`
} }
......
...@@ -20,8 +20,6 @@ func TestMigrations(t *testing.T) { ...@@ -20,8 +20,6 @@ func TestMigrations(t *testing.T) {
testDBs := []sqlutil.TestDB{ testDBs := []sqlutil.TestDB{
sqlutil.TestDB_Sqlite3, sqlutil.TestDB_Sqlite3,
// sqlutil.TestDB_Mysql,
// sqlutil.TestDB_Postgres,
} }
for _, testDB := range testDBs { 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