-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4a6545e
commit 930d0fe
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/claustra01/calendeye/db" | ||
) | ||
|
||
type TokenResponseBody struct { | ||
RefreshToken string `json:"refresh_token"` | ||
} | ||
|
||
func UpdateRefreshToken(w http.ResponseWriter, req *http.Request) { | ||
defer func() { _ = req.Body.Close() }() | ||
|
||
id := req.URL.Query().Get("id") | ||
body, err := ioutil.ReadAll(req.Body) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
var token TokenResponseBody | ||
err = json.Unmarshal(body, &token) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
err = db.UpdateRefreshToken(id, token.RefreshToken) | ||
if err == db.ErrNoRecord { | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} else if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("Refresh token updated")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/claustra01/calendeye/db" | ||
) | ||
|
||
func GetUser(w http.ResponseWriter, req *http.Request) { | ||
defer func() { _ = req.Body.Close() }() | ||
|
||
id := req.URL.Query().Get("id") | ||
if id == "" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
w.Write([]byte("id is required")) | ||
return | ||
} | ||
|
||
user, err := db.GetUser(id) | ||
if err == db.ErrNoRecord { | ||
w.WriteHeader(http.StatusNotFound) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} else if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
jsonUser, err := json.Marshal(user) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(jsonUser)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters