feat: auto login when register successfully

This commit is contained in:
RockYang 2023-10-13 16:27:40 +08:00
parent d7e815d2bb
commit df2fc9d77c
7 changed files with 40 additions and 15 deletions

View File

@ -80,14 +80,6 @@ func (h *UserHandler) Register(c *gin.Context) {
return
}
// 默认订阅所有角色
var chatRoles []model.ChatRole
h.db.Find(&chatRoles)
var roleKeys = make([]string, 0)
for _, r := range chatRoles {
roleKeys = append(roleKeys, r.Key)
}
salt := utils.RandString(8)
user := model.User{
Password: utils.GenPassword(data.Password, salt),
@ -95,7 +87,7 @@ func (h *UserHandler) Register(c *gin.Context) {
Salt: salt,
Status: true,
Mobile: data.Mobile,
ChatRoles: utils.JsonEncode(roleKeys),
ChatRoles: utils.JsonEncode([]string{"gpt"}), // 默认只订阅通用助手角色
ChatConfig: utils.JsonEncode(types.UserChatConfig{
ApiKeys: map[types.Platform]string{
types.OpenAI: "",
@ -116,7 +108,24 @@ func (h *UserHandler) Register(c *gin.Context) {
if h.App.SysConfig.EnabledMsg {
_ = h.leveldb.Delete(key) // 注册成功,删除短信验证码
}
resp.SUCCESS(c, user)
// 自动登录创建 token
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"user_id": user.Id,
"expired": time.Now().Add(time.Second * time.Duration(h.App.Config.Session.MaxAge)).Unix(),
})
tokenString, err := token.SignedString([]byte(h.App.Config.Session.SecretKey))
if err != nil {
resp.ERROR(c, "Failed to generate token, "+err.Error())
return
}
// 保存到 redis
key = fmt.Sprintf("users/%d", user.Id)
if _, err := h.redis.Set(c, key, tokenString, 0).Result(); err != nil {
resp.ERROR(c, "error with save token: "+err.Error())
return
}
resp.SUCCESS(c, tokenString)
}
// Login 用户登录

View File

@ -41,10 +41,13 @@
.page-images-wall .inner .waterfall .list-item .prompt {
display: none;
position: absolute;
width: 180px;
bottom: 0;
left: 0;
color: #fff;
padding: 10px 10px 20px 10px;
line-height: 1.2;
border-top-right-radius: 10px;
background-color: rgba(10,10,10,0.7);
}
.page-images-wall .inner .waterfall .list-item .prompt .el-icon {

View File

@ -60,10 +60,13 @@
.prompt {
display none
position absolute
width 180px
bottom 0
left 0
color #ffffff
padding 10px 10px 20px 10px
line-height 1.2
border-top-right-radius 10px
background-color rgba(10, 10, 10, 0.7)
.el-icon {

View File

@ -88,7 +88,7 @@ const capabilities = ref([
value: ""
},
{
text: "国产大语言模型支持,GLM2 模型接入中",
text: "国产大语言模型支持,百度文心科大讯飞ChatGLM...",
value: ""
},
{

View File

@ -283,6 +283,7 @@
</ItemList>
<el-empty :image-size="100" v-else/>
</div>
<h2>创作记录</h2>
<div class="finish-job-list">
<ItemList :items="finishedJobs" v-if="finishedJobs.length > 0">
@ -356,6 +357,8 @@
</div>
</template>
</ItemList>
<el-empty :image-size="100" v-else/>
</div> <!-- end finish job list-->
</div>

View File

@ -372,6 +372,7 @@
</div>
</template>
</ItemList>
<el-empty :image-size="100" v-else/>
</div> <!-- end finish job list-->
</div>
@ -619,14 +620,14 @@ onMounted(() => {
checkSession().then(user => {
imgCalls.value = user['img_calls']
//
httpGet("/api/sd/jobs?status=0").then(res => {
httpGet(`/api/sd/jobs?status=0&user_id=${user['id']}`).then(res => {
runningJobs.value = res.data
}).catch(e => {
ElMessage.error("获取任务失败:" + e.message)
})
//
httpGet("/api/sd/jobs?status=1").then(res => {
httpGet(`/api/sd/jobs?status=1&user_id=${user['id']}`).then(res => {
finishedJobs.value = res.data
previewImgList.value = []
for (let index in finishedJobs.value) {

View File

@ -109,6 +109,7 @@ import SendMsg from "@/components/SendMsg.vue";
import {validateMobile} from "@/utils/validate";
import {isMobile} from "@/utils/libs";
import SendMsgMobile from "@/components/SendMsg.vue";
import {setUserToken} from "@/store/session";
const router = useRouter();
const title = ref('ChatGPT-PLUS 用户注册');
@ -144,8 +145,13 @@ const register = function () {
return ElMessage.error('请输入短信验证码');
}
formData.value.code = parseInt(formData.value.code)
httpPost('/api/user/register', formData.value).then(() => {
ElMessage.success({"message": "注册成功,即将跳转到登录页...", onClose: () => router.push("/login")})
httpPost('/api/user/register', formData.value).then((res) => {
setUserToken(res.data)
ElMessage.success({
"message": "注册成功,即将跳转到对话主界面...",
onClose: () => router.push("/chat"),
duration: 1000
})
}).catch((e) => {
ElMessage.error('注册失败,' + e.message)
})