diff --git a/components/LoginModal/index.tsx b/components/LoginModal/index.tsx index e74f44f4..0e00fc58 100644 --- a/components/LoginModal/index.tsx +++ b/components/LoginModal/index.tsx @@ -1,5 +1,6 @@ import React, { useState } from 'react' import { useCookies } from 'react-cookie' +import { AxiosResponse } from 'axios' import * as Dialog from '@radix-ui/react-dialog' @@ -94,27 +95,42 @@ const LoginModal = (props: Props) => { if (formValid) { api.login(body) - .then((response) => { - const cookieObj = { - user_id: response.data.user.id, - username: response.data.user.username, - access_token: response.data.access_token - } - - setCookies('user', cookieObj, { path: '/'}) - accountState.account.authorized = true - accountState.account.user = { - id: cookieObj.user_id, - username: cookieObj.username - } - - setOpen(false) - }, (error) => { - console.error(error) - }) + .then(response => response.data.user.id) + .then(id => fetchUserInfo(id)) + .then(infoResponse => storeUserInfo(infoResponse)) } } + function fetchUserInfo(id: string) { + return api.userInfo(id) + } + + function storeUserInfo(response: AxiosResponse) { + const user = response.data.user + + const cookieObj = { + user_id: user.id, + username: user.username, + picture: user.picture.picture, + element: user.picture.element, + language: user.language, + access_token: response.data.access_token + } + + setCookies('user', cookieObj, { path: '/'}) + + accountState.account.language = user.language + accountState.account.user = { + id: user.id, + username: user.username, + picture: user.picture.picture, + element: user.picture.element + } + + accountState.account.authorized = true + setOpen(false) + } + function openChange(open: boolean) { setOpen(open) setErrors({ @@ -158,7 +174,7 @@ const LoginModal = (props: Props) => { ref={passwordInput} /> - +