|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "github.com/supertokens/supertokens-golang/recipe/dashboard/dashboardmodels" |
| 6 | + "github.com/supertokens/supertokens-golang/supertokens" |
| 7 | +) |
| 8 | + |
| 9 | +type signInPostResponse struct { |
| 10 | + Status string `json:"status"` |
| 11 | + SessionId string `json:"sessionId,omitempty"` |
| 12 | + Message string `json:"message,omitempty"` |
| 13 | +} |
| 14 | + |
| 15 | +type signInRequestBody struct { |
| 16 | + Email *string `json:"email"` |
| 17 | + Password *string `json:"password"` |
| 18 | +} |
| 19 | + |
| 20 | +func SignInPost(apiInterface dashboardmodels.APIInterface, options dashboardmodels.APIOptions) error { |
| 21 | + body, err := supertokens.ReadFromRequest(options.Req) |
| 22 | + |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + |
| 27 | + var readBody signInRequestBody |
| 28 | + err = json.Unmarshal(body, &readBody) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + |
| 33 | + if readBody.Email == nil { |
| 34 | + return supertokens.BadInputError{ |
| 35 | + Msg: "Required parameter 'email' is missing", |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + if readBody.Password == nil { |
| 40 | + return supertokens.BadInputError{ |
| 41 | + Msg: "Required parameter 'password' is missing", |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + querier, querierErr := supertokens.GetNewQuerierInstanceOrThrowError("dashboard") |
| 46 | + |
| 47 | + if querierErr != nil { |
| 48 | + return querierErr |
| 49 | + } |
| 50 | + |
| 51 | + apiResponse, apiErr := querier.SendPostRequest("/recipe/dashboard/signin", map[string]interface{}{ |
| 52 | + "email": *readBody.Email, |
| 53 | + "password": *readBody.Password, |
| 54 | + }) |
| 55 | + |
| 56 | + if apiErr != nil { |
| 57 | + return apiErr |
| 58 | + } |
| 59 | + |
| 60 | + status := apiResponse["status"] |
| 61 | + |
| 62 | + if status == "OK" { |
| 63 | + return supertokens.Send200Response(options.Res, map[string]interface{}{ |
| 64 | + "status": "OK", |
| 65 | + "sessionId": apiResponse["sessionId"].(string), |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + if status == "USER_SUSPENDED_ERROR" { |
| 70 | + return supertokens.Send200Response(options.Res, map[string]interface{}{ |
| 71 | + "status": "USER_SUSPENDED_ERROR", |
| 72 | + "message": apiResponse["message"].(string), |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + return supertokens.Send200Response(options.Res, map[string]interface{}{ |
| 77 | + "status": "INVAlID_CREDENTIALS_ERROR", |
| 78 | + }) |
| 79 | +} |
0 commit comments