因此,我正在编写一个API,用于注册/登录/注销用户。
为此,我使用了以下包https://github.com/ory/client-go
下面的代码应该注册用户
var newIdentity models.NewIdentity
err := fctx.BodyParser(&newIdentity) // request body to struct
if err != nil {
c.Log.Error("Error occurred during unmarshalling. Error: %s", err.Error())
return err
}
adminCreateIdentityBody := *ory.NewAdminCreateIdentityBody(
"default",
map[string]interface{}{
"email": newIdentity.Email,
// I think that somewhere here I should insert a password, but it doesn't work
"name": map[string]string{
"first": newIdentity.Name.First,
"last": newIdentity.Name.Last,
},
})
createdIdentity, resp, err := c.OryAdminApiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()问题是,我不太明白如何为我正在创建的用户将会话发送到客户端
我也找不到如何为在adminCreateIdentityBody中创建的用户写下密码。
发布于 2022-06-30 00:14:33
这并不能回答你所有的问题,但我想出了如何在后端创建用户。我正在使用Gin进行网络路由,所以如果您不是,那么有些部分可能是不同的。此外,这段代码有点脏,但它确实设置了从简单HTML post表单发送的密码:
import (
/* your other imports here */
kratosclient "github.com/ory/kratos-client-go"
)
func Route(dsn string) {
router := gin.Default()
router.POST("/signup", func(c *gin.Context) {
log.Println("Sign up!")
var signupRequest SignupRequest
error := c.Bind(&signupRequest)
if error != nil {
// Required parameter wasn't there; client error.
// status = http.StatusBadRequest
// response = ErrorResponse()
log.Println("Parameter error - ", error)
return
}
configuration := kratosclient.NewConfiguration()
configuration.Servers = []kratosclient.ServerConfiguration{
{
URL: "http://host.docker.internal:4434", // Kratos Admin API - when running Kratos container on "same machine/host/localhost", as this app
// URL: "http://127.0.0.1:4434", // Kratos Admin API
},
}
apiClient := kratosclient.NewAPIClient(configuration)
// Set password
myPw := *kratosclient.NewAdminCreateIdentityImportCredentialsPassword()
myConfig := kratosclient.AdminCreateIdentityImportCredentialsPasswordConfig{}
myPw.SetConfig(myConfig)
myPw.Config.SetPassword(signupRequest.Password)
adminCreateIdentityBody := *kratosclient.NewAdminCreateIdentityBody(
"default",
map[string]interface{}{
"email": signupRequest.Email,
"name": map[string]string{
"first": "foo",
"last": "bar",
},
},
)
myCredentials := kratosclient.AdminIdentityImportCredentials{}
myCredentials.SetPassword(myPw)
adminCreateIdentityBody.SetCredentials(myCredentials)
createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminCreateIdentity``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
// response from `AdminCreateIdentity`: Identity
fmt.Fprintf(os.Stdout, "Created identity with ID: %v\n", createdIdentity.Id)
getIdentity, r, err := apiClient.V0alpha2Api.AdminGetIdentity(context.Background(), createdIdentity.Id).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminGetIdentity``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
fmt.Fprintf(os.Stdout, "Email for identity with id %v. Traits %v\n", createdIdentity.Id, getIdentity.Traits)
c.HTML(http.StatusOK, "signupdone.tmpl", gin.H{
"email": signupRequest.Email,
})
// We could delete the identity like below:
// r, err = apiClient.V0alpha2Api.AdminDeleteIdentity(context.Background(), getIdentity.Id).Execute()
// if err != nil {
// fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminDeleteIdentity``: %v\n", err)
// fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
// }
// fmt.Println("Successfully Removed identity")
})
}
type SignupRequest struct {
Email string `form:"email" binding:"required"`
Password string `form:"pw" binding:"required"`
}我几乎还没有接触到Go,所以可能有一些更好的方法来描述事情,但这在我的容器上确实有效。
https://stackoverflow.com/questions/72728144
复制相似问题