chore: started working on the api
This commit is contained in:
36
users.go
36
users.go
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -79,8 +78,39 @@ func usersEndpints(db *sql.DB, handle *Handle) {
|
||||
handle.GetHTML("/login", AnswerTemplate("login.html", nil, 0))
|
||||
handle.Post("/login", func(w http.ResponseWriter, r *http.Request, c *Context) *Error {
|
||||
if c.Mode == JSON {
|
||||
fmt.Println("Handle JSON")
|
||||
return &Error{Code: 404}
|
||||
|
||||
type UserLogin struct {
|
||||
Email string `json:email`
|
||||
Password string `json:password`
|
||||
}
|
||||
|
||||
var dat UserLogin
|
||||
|
||||
if err := c.ToJSON(r, &dat); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
/*if (dat["email"] == nil || dat["password"] == nil) {
|
||||
// TODO improve this
|
||||
c.Logger.Warn("Email or password are empty")
|
||||
return c.Error500(nil)
|
||||
}*/
|
||||
|
||||
// TODO Give this to the generateToken function
|
||||
expiration := time.Now().Add(24 * time.Hour)
|
||||
token, login := generateToken(db, dat.Email, dat.Password)
|
||||
if !login {
|
||||
return c.SendJSONStatus(w, http.StatusUnauthorized, "Email or password are incorrect")
|
||||
}
|
||||
|
||||
|
||||
cookie := &http.Cookie{Name: "auth", Value: token, HttpOnly: false, Expires: expiration}
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
w.Header().Set("Location", "/")
|
||||
w.WriteHeader(http.StatusSeeOther)
|
||||
return nil
|
||||
}
|
||||
|
||||
r.ParseForm()
|
||||
|
||||
Reference in New Issue
Block a user