Skip to content

Commit

Permalink
修改登录逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuaige1234567 committed Dec 9, 2023
1 parent 4a1c1cf commit 351c5fb
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 78 deletions.
3 changes: 1 addition & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, {useEffect, useState} from 'react';
import logo from './logo.svg';
import React from 'react';
import './App.css';
import {Layout} from 'antd';
import Sider from './components/sider';
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/components/sider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ interface SiderProps {
export default function Sider() {
const navigate = useNavigate();
const {isAuth, principal, logIn} = useAuth()
console.log('isLogIn : ', isAuth);
// if(props.authClient != undefined) {
// console.log(props.authClient);
// getUserProfile(props.authClient.getIdentity());
// }

const onClick = (info: MenuInfo) => {
if (info.key === '1') {
Expand Down
84 changes: 14 additions & 70 deletions frontend/src/routes/profile.tsx
Original file line number Diff line number Diff line change
@@ -1,84 +1,32 @@
import {useState, useEffect} from 'react';
import {Layout, Image, Typography, Avatar, Flex, Space, Button, Modal, notification} from 'antd';
import Sider from '../components/sider';
import Post from '../components/post';
import {AuthClient} from "@dfinity/auth-client";
import ProfileForm from '../components/form';
import User from '../actors/user';
import {Profile} from '../declarations/user/user.did';
import {useAuth} from "../utils/useAuth";

type NotificationType = 'success' | 'info' | 'warning' | 'error';

export default function UserProfile() {
const [authClient, setAuthClient] = useState<AuthClient | undefined>();
const [isLogin, setIsLogin] = useState<Boolean>(false);
const {isAuth, identity, principal} = useAuth()
const [isModalOpen, setIsModalOpen] = useState(false);
const [userProfile, setUserProfile] = useState<Profile | undefined>();

const handleLogIn = async () => {
const _authClient = await AuthClient.create();
_authClient.login({
maxTimeToLive: BigInt(7 * 24 * 60 * 60 * 1000 * 1000 * 1000),
onSuccess: async () => {
setAuthClient(_authClient);
setIsLogin(true);
localStorage.setItem('authClient', JSON.stringify(_authClient));
localStorage.setItem('isLogin', JSON.stringify(true));
}
});
}

useEffect(() => {

const loadAuthFromLocalStorage = async () => {
const storedAuthClient = localStorage.getItem('authClient');
const storedIsLogin = localStorage.getItem('isLogin');

if (storedAuthClient && storedIsLogin) {
const _auth = JSON.parse(storedAuthClient);
console.log('_auth : ', _auth);
try {
const createdAuthClient = await AuthClient.create({
identity: _auth._identity,
idleOptions: _auth._idleManager,
storage: _auth._storage,
});
setAuthClient(createdAuthClient);
setIsLogin(JSON.parse(storedIsLogin));
} catch (error) {
console.error('Error creating AuthClient:', error);
if (!isAuth || !identity || !principal) return
const userActor = new User(identity);
userActor.actor.getProfile(principal).then(
(result) => {
if (result.length > 0) {
setUserProfile(result[0]);
}
},
(error) => {
console.error('query profile error : ', error);
}
};

// if (storedAuthClient && storedIsLogin) {
// const _auth = JSON.parse(storedAuthClient);
// console.log('_auth : ', _auth);
// setAuthClient(
// awaiAuthClient.create({
// identity: _auth._identity,
// idleOptions: _auth._idleManager,
// storage: _auth._storage
// })
// );
// setIsLogin(JSON.parse(storedIsLogin));
// }

if (isLogin) {
const userActor = new User(authClient!.getIdentity());
const userPrincipal = authClient!.getIdentity().getPrincipal();
userActor.actor.getProfile(userPrincipal).then(
(result) => {
if (result.length > 0) {
setUserProfile(result[0]);
}
},
(error) => {
console.error('query profile error : ', error);
}
);
}
}, [isLogin]);
);
}, [isAuth, identity]);

const [api, contextHolder] = notification.useNotification();

Expand All @@ -92,17 +40,13 @@ export default function UserProfile() {
};

const handleEditProfile = () => {
if (!isLogin) {
if (!isAuth) {
openNotificationWithIcon('error')
} else {
setIsModalOpen(true);
}
};

useEffect(() => {

}, [authClient, isLogin]);

return (
<>
<Layout.Content style={{
Expand Down
1 change: 0 additions & 1 deletion frontend/src/utils/useAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const useProvideAuth = (authClient: IIForIdentity): Props => {
if (!authClient) return {message: "connect error"};
const identity = await authClient.login();
const principal = identity.getPrincipal();
// const subAccountId = principalToAccountIdentifier(principal, 0);
setPrincipal(principal);
if (identity) {
_setIdentity(_identity);
Expand Down

0 comments on commit 351c5fb

Please sign in to comment.