quickshare/src/client/singleuser.go
Hexxa ba6a5373d1
Web client refinement (#16)
* fix(files/handler): add base64 decode for content

* fix(singleuser): pick user name from jwt token and encode content

* fix(singleuser): add public path check, abstract user info from token

* fix(singleuser): update singleuser client

* fix(server): fix test and enable auth by default

* feat(client/web): add web client

* fix(client/web): refine css styles

* fix(client/web): refine styles

* fix(client/web): refine styles, add test and fix bugs

* test(client/web): add web client tests

* fix(client/web): refactor client interface and enhance the robustness

* chore(client/web): ignore js bundles

* test(files): call sync before check

Co-authored-by: Jia He <jiah@nvidia.com>
2020-12-16 23:39:26 +08:00

51 lines
1.1 KiB
Go

package client
import (
"fmt"
"net/http"
su "github.com/ihexxa/quickshare/src/handlers/singleuserhdr"
"github.com/parnurzeal/gorequest"
)
type SingleUserClient struct {
addr string
r *gorequest.SuperAgent
}
func NewSingleUserClient(addr string) *SingleUserClient {
gr := gorequest.New()
return &SingleUserClient{
addr: addr,
r: gr,
}
}
func (cl *SingleUserClient) url(urlpath string) string {
return fmt.Sprintf("%s%s", cl.addr, urlpath)
}
func (cl *SingleUserClient) Login(user, pwd string) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/login")).
Send(su.LoginReq{
User: user,
Pwd: pwd,
}).
End()
}
func (cl *SingleUserClient) Logout(token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Post(cl.url("/v1/users/logout")).
AddCookie(token).
End()
}
func (cl *SingleUserClient) SetPwd(oldPwd, newPwd string, token *http.Cookie) (*http.Response, string, []error) {
return cl.r.Patch(cl.url("/v1/users/pwd")).
Send(su.SetPwdReq{
OldPwd: oldPwd,
NewPwd: newPwd,
}).
AddCookie(token).
End()
}