Skip to content

Commit

Permalink
fix: telegram register
Browse files Browse the repository at this point in the history
  • Loading branch information
Calcium-Ion committed Dec 7, 2024
1 parent 904a185 commit dd293f8
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion web/src/components/RegisterForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { API, getLogo, showError, showInfo, showSuccess, updateAPI } from '../helpers';
import Turnstile from 'react-turnstile';
Expand All @@ -11,6 +11,7 @@ import LinuxDoIcon from './LinuxDoIcon.js';
import WeChatIcon from './WeChatIcon.js';
import TelegramLoginButton from 'react-telegram-login/src';
import { setUserData } from '../helpers/data.js';
import { UserContext } from '../context/User/index.js';

const RegisterForm = () => {
const [inputs, setInputs] = useState({
Expand All @@ -22,6 +23,7 @@ const RegisterForm = () => {
});
const { username, password, password2 } = inputs;
const [showEmailVerification, setShowEmailVerification] = useState(false);
const [userState, userDispatch] = useContext(UserContext);
const [turnstileEnabled, setTurnstileEnabled] = useState(false);
const [turnstileSiteKey, setTurnstileSiteKey] = useState('');
const [turnstileToken, setTurnstileToken] = useState('');
Expand Down Expand Up @@ -133,6 +135,38 @@ const RegisterForm = () => {
setLoading(false);
};

const onTelegramLoginClicked = async (response) => {
const fields = [
'id',
'first_name',
'last_name',
'username',
'photo_url',
'auth_date',
'hash',
'lang',
];
const params = {};
fields.forEach((field) => {
if (response[field]) {
params[field] = response[field];
}
});
const res = await API.get(`/api/oauth/telegram/login`, { params });
const { success, message, data } = res.data;
if (success) {
userDispatch({ type: 'login', payload: data });
localStorage.setItem('user', JSON.stringify(data));
showSuccess('登录成功!');
setUserData(data);
updateAPI();
navigate('/');
} else {
showError(message);
}
};


return (
<div>
<Layout>
Expand Down

0 comments on commit dd293f8

Please sign in to comment.