|
@@ -1,20 +1,46 @@
|
|
|
import { currentUserActions } from '../../stores/reducers/current-user/slice';
|
|
|
import { useAppDispatch, useAppSelector } from '../../stores/store';
|
|
|
import { UserProfilePB } from '../../../services/backend/events/flowy-user';
|
|
|
-import { AuthBackendService } from '../../stores/effects/user/user_bd_svc';
|
|
|
+import { AuthBackendService, UserBackendService } from '../../stores/effects/user/user_bd_svc';
|
|
|
import { FolderEventReadCurrentWorkspace } from '../../../services/backend/events/flowy-folder';
|
|
|
import { WorkspaceSettingPB } from '../../../services/backend/models/flowy-folder/workspace';
|
|
|
+import { Log } from '../../utils/log';
|
|
|
|
|
|
export const useAuth = () => {
|
|
|
const dispatch = useAppDispatch();
|
|
|
const currentUser = useAppSelector((state) => state.currentUser);
|
|
|
const authBackendService = new AuthBackendService();
|
|
|
|
|
|
+ async function checkUser() {
|
|
|
+ const result = await UserBackendService.checkUser();
|
|
|
+ if (result.ok) {
|
|
|
+ const userProfile = result.val;
|
|
|
+ const workspaceSetting = await _openWorkspace().then((r) => {
|
|
|
+ if (r.ok) {
|
|
|
+ return r.val;
|
|
|
+ } else {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ dispatch(
|
|
|
+ currentUserActions.checkUser({
|
|
|
+ id: userProfile.id,
|
|
|
+ token: userProfile.token,
|
|
|
+ email: userProfile.email,
|
|
|
+ displayName: userProfile.name,
|
|
|
+ isAuthenticated: true,
|
|
|
+ workspaceSetting: workspaceSetting,
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
async function register(email: string, password: string, name: string): Promise<UserProfilePB> {
|
|
|
const authResult = await authBackendService.signUp({ email, password, name });
|
|
|
|
|
|
if (authResult.ok) {
|
|
|
- const { id, token } = authResult.val;
|
|
|
+ const userProfile = authResult.val;
|
|
|
// Get the workspace setting after user registered. The workspace setting
|
|
|
// contains the latest visiting view and the current workspace data.
|
|
|
const openWorkspaceResult = await _openWorkspace();
|
|
@@ -22,10 +48,10 @@ export const useAuth = () => {
|
|
|
const workspaceSetting: WorkspaceSettingPB = openWorkspaceResult.val;
|
|
|
dispatch(
|
|
|
currentUserActions.updateUser({
|
|
|
- id: id,
|
|
|
- token: token,
|
|
|
- email,
|
|
|
- displayName: name,
|
|
|
+ id: userProfile.id,
|
|
|
+ token: userProfile.token,
|
|
|
+ email: userProfile.email,
|
|
|
+ displayName: userProfile.name,
|
|
|
isAuthenticated: true,
|
|
|
workspaceSetting: workspaceSetting,
|
|
|
})
|
|
@@ -33,7 +59,7 @@ export const useAuth = () => {
|
|
|
}
|
|
|
return authResult.val;
|
|
|
} else {
|
|
|
- console.error(authResult.val.msg);
|
|
|
+ Log.error(authResult.val.msg);
|
|
|
throw new Error(authResult.val.msg);
|
|
|
}
|
|
|
}
|
|
@@ -53,7 +79,7 @@ export const useAuth = () => {
|
|
|
);
|
|
|
return result.val;
|
|
|
} else {
|
|
|
- console.error(result.val.msg);
|
|
|
+ Log.error(result.val.msg);
|
|
|
throw new Error(result.val.msg);
|
|
|
}
|
|
|
}
|
|
@@ -67,5 +93,5 @@ export const useAuth = () => {
|
|
|
return FolderEventReadCurrentWorkspace();
|
|
|
}
|
|
|
|
|
|
- return { currentUser, register, login, logout };
|
|
|
+ return { currentUser, checkUser, register, login, logout };
|
|
|
};
|