-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
47 lines (36 loc) · 959 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package server
import (
"net/http"
"github.com/haleyrc/pkg/errors"
"github.com/haleyrc/pkg/json"
"github.com/haleyrc/cheevos/domain"
"github.com/haleyrc/cheevos/internal/lib/web"
"github.com/haleyrc/cheevos/internal/password"
)
type AuthServer struct {
Auth domain.AuthService
}
type SignUpRequest struct {
Username string `json:"username"`
Password password.Password `json:"password"`
}
type SignUpResponse struct {
ID string `json:"id"`
Username string `json:"username"`
}
func (as *AuthServer) SignUp(w http.ResponseWriter, r *http.Request) (web.Data, error) {
ctx := r.Context()
var req SignUpRequest
if err := json.Decode(&req, r.Body); err != nil {
return nil, domain.NewBadRequestError(err)
}
user, err := as.Auth.SignUp(ctx, req.Username, req.Password)
if err != nil {
return nil, errors.WrapError(err)
}
resp := SignUpResponse{
ID: user.ID,
Username: user.Username,
}
return resp, nil
}