Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: finish login page #24

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 88 additions & 2 deletions src/content/drawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import Drawer from '@mui/material/Drawer';
import IconButton from '@mui/material/IconButton';
import { styled } from '@mui/material/styles';

import { ProfileData } from '../../service/login/me';
import { TwitterOauth2Data } from '../../service/login/twiterOuth2';
import http, { ResultData } from '../../service/request';
import useGlobalStore from '../../store/useGlobalStore';
import Profile from '../../welcome/Profile';
import Wallet from '../../welcome/Wallet';
import ProfileModal from '../../welcome/Wallet/Profile';
Expand Down Expand Up @@ -47,6 +51,7 @@ const Main = styled('main', { shouldForwardProp: (prop) => prop !== 'open' })<{

export default function PersistentDrawerRight() {
const [open, setOpen] = React.useState(false);
const [loginLoading, setLoginLoading] = React.useState(false);

const handleDrawerOpen = () => {
setOpen(true);
Expand All @@ -58,6 +63,87 @@ export default function PersistentDrawerRight() {

const [pageState, setPageState] = React.useState('login');

React.useEffect(() => {
// 获取当前 URL 中的参数
const urlParams = new URLSearchParams(window.location.search);
const xfansToken = urlParams.get('xfans_token');

console.log(xfansToken);

// 获取 xfans_token 参数的值
const localStorageToken = localStorage.getItem('xfans-token');

const loginState = localStorage.getItem('xfans-login-state');
const shouldOpenStateList: string[] = ['waitingRedirect', 'waitingInvite'];

if (shouldOpenStateList.includes(String(loginState))) {
setOpen(true);
}
if (localStorageToken && localStorageToken.length > 0) {
// 已经有 token 的情况,登录判断 invite 状态
useGlobalStore.setState({ token: localStorageToken });
checkProfileData();
} else {
// 没有 token 的状态,检查 url 是否存在 token
if (xfansToken) {
// 登录看是否有效,拿到 invite 状态
useGlobalStore.setState({ token: xfansToken });
localStorage.setItem('xfans-token', xfansToken);
clickLogin();
}
}
}, []);

const checkProfileData = async () => {
// https://test-xfans-api.d.buidlerdao.xyz/api/user/me
const profileData = (await http.get(`/api/user/me`)) as ResultData<ProfileData>;
if (profileData.code === 0) {
if (profileData.data.isActive) {
setPageState('profile');
return 'active';
} else {
setPageState('invite');
return 'waiting invite';
}
} else {
return profileData.message;
}
};

const clickLogin = async () => {
setLoginLoading(true);
// 设置 waiting redirect 缓存
localStorage.setItem('xfans-login-state', 'waitingRedirect');

// 跳转 login link https://test-xfans-api.d.buidlerdao.xyz/api/user/twitter-oauth2
const link = (await http.get(`/api/user/twitter-oauth2`)) as ResultData<TwitterOauth2Data>;
console.log(link);
if (link.code === 0) {
window.location.href = link.data.authorizationUrl;
}
};

const clickActivate = async (inviteCode: string) => {
// https://test-xfans-api.d.buidlerdao.xyz/api/user/activate
const activateData = (await http.post(`api/user/activate`, {
inviteCode: inviteCode,
})) as ResultData;
if (activateData.code === 0) {
useGlobalStore.setState({
message: 'congratulation!',
messageType: 'success',
messageOpen: true,
});
setPageState('congratulation');
} else {
useGlobalStore.setState({
message: 'invite code error',
messageType: 'error',
messageOpen: true,
});
}
};

return (
<Box sx={{ display: 'flex' }}>
<ProfileModal />
Expand Down Expand Up @@ -91,10 +177,10 @@ export default function PersistentDrawerRight() {
</div>
<Divider orientation="vertical" flexItem />
{pageState === 'login' && (
<SignInWithXPage handleButtonClick={() => setPageState('invite')} />
<SignInWithXPage showLoading={loginLoading} handleButtonClick={() => clickLogin()} />
)}
{pageState === 'invite' && (
<InvitePage handleButtonClick={() => setPageState('congratulation')} />
<InvitePage handleButtonClick={(inviteCode) => clickActivate(inviteCode)} />
)}
{pageState === 'congratulation' && (
<CongratulationPage handleButtonClick={() => setPageState('profile')} />
Expand Down
33 changes: 30 additions & 3 deletions src/content/loginPage/signInWithXPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import '../../tailwind.css';

interface SignInWithXPageProps {
handleButtonClick: () => void; // 定义一个函数类型的属性
showLoading: boolean;
}

const SignInWithXPage: FC<SignInWithXPageProps> = ({ handleButtonClick }) => {
const SignInWithXPage: FC<SignInWithXPageProps> = ({ handleButtonClick, showLoading }) => {
return (
<div className="w-full justify-center text-center items-center min-h-screen">
<img
Expand All @@ -23,8 +24,34 @@ const SignInWithXPage: FC<SignInWithXPageProps> = ({ handleButtonClick }) => {
Empower early creators, make the creator economy thrive, and build a simple, yet excellent
crypto social experience on Twitter.
</p>
<NextButton variant="contained" disableElevation onClick={handleButtonClick}>
Login With X
<NextButton
variant="contained"
disableElevation
onClick={handleButtonClick}
disabled={showLoading}
>
{showLoading ? (
<div className="px-[60px]">
<svg
aria-hidden="true"
className="w-7 h-7 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
</div>
) : (
'Login With X'
)}
</NextButton>
</div>
);
Expand Down
17 changes: 17 additions & 0 deletions src/service/login/me.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface ProfileData {
id: number;
walletAddress: string;
username: string;
twitterId: string;
twitterUsername: string;
avatar: string;
// supply: Supply;
// holders: Holders;
// holdValue: HoldValue;
// tradingFeeEarned: TradingFeeEarned;
// tradingVolume: TradingVolume;
// rewardEarned: RewardEarned;
// rewardUnClaimed: RewardUnClaimed;
isActive: boolean;
updatedAt: number;
}
3 changes: 3 additions & 0 deletions src/service/login/twiterOuth2.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface TwitterOauth2 {
authorizationUrl: string;
}
1 change: 0 additions & 1 deletion src/welcome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import { createRoot } from 'react-dom/client';

import Profile from './Profile';
import Wallet from './Wallet';

import '../tailwind.css';

Expand Down
Loading