Skip to content

Commit

Permalink
integrate auth
Browse files Browse the repository at this point in the history
  • Loading branch information
zuies committed Sep 26, 2023
1 parent 107929d commit 56b8f33
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/layouts/defaultLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export const defaultLayout = ({ children }: IDefaultLayoutProps) => {
const router = useRouter();
const currentRoute = router.pathname;

const { getGuilds, getGuildInfoByDiscord } = useAppStore();
const { getGuilds, getGuildInfoByDiscord, authorizeTwitter } = useAppStore();
const [openDialog, setOpenDialog] = useState<boolean>(false);

const user = StorageService.readLocalStorage<IUser>('user');

useEffect(() => {
const user = StorageService.readLocalStorage<IUser>('user');
if (user) {
const { guildId } = user.guild;
getGuilds();
Expand Down Expand Up @@ -154,6 +155,7 @@ export const defaultLayout = ({ children }: IDefaultLayoutProps) => {
<TcButton
text={'Connect Twitter account'}
variant="contained"
onClick={() => authorizeTwitter(user?.token.accessToken)}
/>
</div>{' '}
</div>
Expand Down
29 changes: 29 additions & 0 deletions src/store/slices/twitterSlice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { StateCreator } from 'zustand';
import { axiosInstance } from '../../axiosInstance';
import ITwitter from '../types/ITwitter';
import { conf } from '../../configs';

const BASE_URL = conf.API_BASE_URL;

const createTwitterSlice: StateCreator<ITwitter> = (set, get) => ({
authorizeTwitter: async (token: string) => {
try {
// Send token to intermediary endpoint
await axiosInstance({
method: 'GET', // adjust as necessary
url: `${BASE_URL}/auth/twitter/login`,
headers: {
Authorization: `Bearer ${token}`,
},
});

// The above request should set some session or cookie authentication
// and then redirect to the Twitter authorization URL.
// Since it's a 302 redirect, you might not need to handle the response directly.
} catch (error) {
console.error('Error in intermediary auth step:', error);
}
},
});

export default createTwitterSlice;
3 changes: 3 additions & 0 deletions src/store/types/ITwitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface ITwitter {
authorizeTwitter: (token: string) => void;
}
2 changes: 2 additions & 0 deletions src/store/useStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import createSettingSlice from './slices/settingSlice';
import createBreakdownsSlice from './slices/breakdownsSlice';
import createMemberInteractionSlice from './slices/memberInteractionSlice';
import communityHealthSlice from './slices/communityHealthSlice';
import twitterSlice from './slices/twitterSlice';

const useAppStore = create<any>()((...a) => ({
...createAuthSlice(...a),
Expand All @@ -13,6 +14,7 @@ const useAppStore = create<any>()((...a) => ({
...createBreakdownsSlice(...a),
...createMemberInteractionSlice(...a),
...communityHealthSlice(...a),
...twitterSlice(...a),
}));

export default useAppStore;

0 comments on commit 56b8f33

Please sign in to comment.