@@ -10,38 +10,26 @@ import (
1010 "specialstandard/internal/errs"
1111
1212 "github.com/goccy/go-json"
13- "github.com/google/uuid"
14- )
15-
16- type userResponse struct {
17- ID uuid.UUID `json:"id"`
18- }
1913
20- type SignInResponse struct {
21- AccessToken string `json:"access_token"`
22- TokenType string `json:"token_type"`
23- ExpiresIn int `json:"expires_in"`
24- RefreshToken string `json:"refresh_token"`
25- User userResponse `json:"user"`
26- Error interface {} `json:"error"`
27- }
14+ "specialstandard/internal/models"
15+ )
2816
29- func SupabaseLogin (cfg * config.Supabase , email string , password string ) (SignInResponse , error ) {
17+ func SupabaseLogin (cfg * config.Supabase , email string , password string , needsEmailVerification bool ) (models. SignInResponse , error ) {
3018 supabaseURL := cfg .URL
3119 serviceroleKey := cfg .ServiceRoleKey
3220
33- payload := Payload {
21+ payload := models. Payload {
3422 Email : email ,
3523 Password : password ,
3624 }
3725 payloadBytes , err := json .Marshal (payload )
3826 if err != nil {
39- return SignInResponse {}, err
27+ return models. SignInResponse {}, err
4028 }
4129
4230 req , err := http .NewRequest ("POST" , fmt .Sprintf ("%s/auth/v1/token?grant_type=password" , supabaseURL ), bytes .NewBuffer (payloadBytes ))
4331 if err != nil {
44- return SignInResponse {}, err
32+ return models. SignInResponse {}, err
4533 }
4634
4735 req .Header .Set ("Content-Type" , "application/json" )
@@ -51,7 +39,7 @@ func SupabaseLogin(cfg *config.Supabase, email string, password string) (SignInR
5139 res , err := Client .Do (req )
5240 if err != nil {
5341 slog .Error ("Failed to execute Request" , "err" , err )
54- return SignInResponse {}, errs .BadRequest ("Failed to execute Request" )
42+ return models. SignInResponse {}, errs .BadRequest ("Failed to execute Request" )
5543 }
5644 defer func () {
5745 _ = res .Body .Close ()
@@ -60,40 +48,42 @@ func SupabaseLogin(cfg *config.Supabase, email string, password string) (SignInR
6048 body , err := io .ReadAll (res .Body )
6149 if err != nil {
6250 slog .Error ("Failed to read response body" , "err" , err )
63- return SignInResponse {}, errs .BadRequest ("Failed to read response body" )
51+ return models. SignInResponse {}, errs .BadRequest ("Failed to read response body" )
6452 }
6553
6654 if res .StatusCode != http .StatusOK {
67- // Try to parse the error response
68- var errorResp struct {
69- Message string `json:"msg"`
70- Error string `json:"error"`
71- }
72-
73- if err := json .Unmarshal (body , & errorResp ); err == nil {
74- // Use the parsed message if available
75- if errorResp .Message != "" {
76- return SignInResponse {}, errs .BadRequest (errorResp .Message )
77- }
78- if errorResp .Error != "" {
79- return SignInResponse {}, errs .BadRequest (errorResp .Error )
80- }
81- }
82-
83- // Fallback to generic message if parsing fails
84- return SignInResponse {}, errs .BadRequest ("Invalid credentials" )
85- }
55+ // Try to parse the error response
56+ var errorResp struct {
57+ Message string `json:"msg"`
58+ Error string `json:"error"`
59+ }
60+
61+ if err := json .Unmarshal (body , & errorResp ); err == nil {
62+ // Use the parsed message if available
63+ if errorResp .Message != "" {
64+ return models.SignInResponse {}, errs .BadRequest (errorResp .Message )
65+ }
66+ if errorResp .Error != "" {
67+ return models.SignInResponse {}, errs .BadRequest (errorResp .Error )
68+ }
69+ }
8670
87- var signInResponse SignInResponse
71+ // Fallback to generic message if parsing fails
72+ return models.SignInResponse {}, errs .BadRequest ("Invalid credentials" )
73+ }
74+
75+ var signInResponse models.SignInResponse
8876 err = json .Unmarshal (body , & signInResponse )
8977 if err != nil {
9078 slog .Error ("Failed to parse response body" , "body" , err )
91- return SignInResponse {}, errs .BadRequest ("Failed to parse response body" )
79+ return models. SignInResponse {}, errs .BadRequest ("Failed to parse response body" )
9280 }
9381
9482 if signInResponse .Error != nil {
95- return SignInResponse {}, errs .BadRequest (fmt .Sprintf ("Sign In Response Error %v" , signInResponse .Error ))
83+ return models. SignInResponse {}, errs .BadRequest (fmt .Sprintf ("Sign In Response Error %v" , signInResponse .Error ))
9684 }
9785
86+ signInResponse .RequiresMFA = needsEmailVerification
87+
9888 return signInResponse , nil
9989}
0 commit comments