Saving token to DB
This commit is contained in:
parent
6b7ce5b719
commit
e7f6983a22
@ -2,10 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"myapp/internal/cards"
|
||||
"myapp/internal/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/stripe/stripe-go/v79"
|
||||
@ -266,16 +268,32 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
// generate the token
|
||||
token, err := models.GenerateToken(user.ID, 24*time.Hour, models.ScopeAuthentication)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// save to DB
|
||||
err = app.DB.InsertToken(token, user)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
app.badRequest(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// send response
|
||||
|
||||
var payload struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Token *models.Token `json:"authentication_token"`
|
||||
}
|
||||
|
||||
payload.Error = false
|
||||
payload.Message = "Success!"
|
||||
payload.Message = fmt.Sprintf("token for %s created", userInput.Email)
|
||||
payload.Token = token
|
||||
|
||||
_ = app.writeJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
@ -40,3 +41,25 @@ func GenerateToken(userID int, ttl time.Duration, scope string) (*Token, error)
|
||||
token.Hash = hash[:]
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (m *DBModel) InsertToken(t *Token, u User) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
stmt := `INSERT INTO tokens
|
||||
(user_id, name, email, token_hash, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)`
|
||||
|
||||
_, err := m.DB.ExecContext(ctx, stmt,
|
||||
u.ID,
|
||||
u.LastName,
|
||||
u.Email,
|
||||
t.Hash,
|
||||
time.Now(),
|
||||
time.Now(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
2
migrations/20240813194454_create_tokens_table.down.fizz
Normal file
2
migrations/20240813194454_create_tokens_table.down.fizz
Normal file
@ -0,0 +1,2 @@
|
||||
drop_table("tokens")
|
||||
|
11
migrations/20240813194454_create_tokens_table.up.fizz
Normal file
11
migrations/20240813194454_create_tokens_table.up.fizz
Normal file
@ -0,0 +1,11 @@
|
||||
create_table("tokens") {
|
||||
t.Column("id", "integer", {primary: true})
|
||||
t.Column("user_id", "integer", {"unsigned": true})
|
||||
t.Column("name", "string", {"size": 255})
|
||||
t.Column("email", "string", {})
|
||||
t.Column("token_hash", "string", {})
|
||||
}
|
||||
|
||||
sql("ALTER TABLE tokens MODIFY token_hash varbinary(255);")
|
||||
sql("ALTER TABLE tokens ALTER COLUMN created_at SET DEFAULT now();")
|
||||
sql("ALTER TABLE tokens ALTER COLUMN updated_at SET DEFAULT now();")
|
Loading…
Reference in New Issue
Block a user