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: Implementação do login social com Github #78

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion frontend/.env

This file was deleted.

8 changes: 7 additions & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import React from 'react';
import Routes from './routes';

import { AuthProvider } from './contexts/AuthContext';

function App() {
return <Routes />;
return (
<AuthProvider>
<Routes />
</AuthProvider>
);
}

export default App;
7 changes: 5 additions & 2 deletions frontend/src/components/Header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faBars, faTimes } from '@fortawesome/free-solid-svg-icons';
import { faGithub } from '@fortawesome/free-brands-svg-icons';

import Logo from '../Logo'
import Logo from '../Logo';

import * as S from './styled';

Expand Down Expand Up @@ -49,7 +49,10 @@ export default function Header() {
</S.StyledLink>
</li>
<li>
<S.ButtonLink activeClassName="is-active" to="/login">
<S.ButtonLink
activeClassName="is-active"
href={`https://github.com/login/oauth/authorize?scope=userl&client_id=${process.env.REACT_APP_CLIENT_ID}`}
>
Entrar
<FontAwesomeIcon
icon={faGithub}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Header/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const StyledLink = styled(NavLink)`
}
`;

export const ButtonLink = styled(NavLink)`
export const ButtonLink = styled.a`
background-color: var(--quaternary);

width: 100px;
Expand Down
15 changes: 15 additions & 0 deletions frontend/src/contexts/AuthContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, { createContext, useState } from 'react';

const AuthContext = createContext({});

const AuthProvider = ({ children }) => {
const [user, setUser] = useState({});

return (
<AuthContext.Provider value={{ user, setUser }}>
{children}
</AuthContext.Provider>
);
};

export { AuthContext, AuthProvider };
48 changes: 48 additions & 0 deletions frontend/src/pages/CallbackPage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable no-shadow */
import React, { useEffect, useCallback, useContext } from 'react';
import { useHistory, useLocation } from 'react-router-dom';

import { AuthContext } from '../../contexts/AuthContext';

import api from '../../services/api';
import { login } from '../../services/authentication';

import Logo from '../../components/Logo';

import * as S from './styled';

export default function CallbackPage() {
const history = useHistory();

const { search } = useLocation();
const searchParam = new URLSearchParams(search);

const code = searchParam.get('code');

const { setUser } = useContext(AuthContext);

const authenticateUserWithGithub = useCallback(
async (code) => {
const response = await api.get(
`http://localhost:3333/auth/${code}`
);
if (response.status === 200 || response.status === 201) {
setUser(response.data.user);
login(response.data.token);
history.push('/dashboard');
}
},
[history, setUser]
);

useEffect(() => {
authenticateUserWithGithub(code);
}, [code, authenticateUserWithGithub]);

return (
<S.Container>
<Logo />
<h2>Aguardando as informações...</h2>
</S.Container>
);
}
15 changes: 15 additions & 0 deletions frontend/src/pages/CallbackPage/styled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import styled from 'styled-components';

export const Container = styled.div`
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
margin-top: 10%;

h2 {
color: white;
margin-top: 20px;
}

`
30 changes: 0 additions & 30 deletions frontend/src/pages/ConstructPage/index.js

This file was deleted.

10 changes: 6 additions & 4 deletions frontend/src/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import { BrowserRouter, Switch, Route, Redirect } from 'react-router-dom';

import { isAuthenticated as logged } from './services/authentication';

import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import Home from './pages/Home';
Expand All @@ -15,15 +17,15 @@ import Footer from './components/Footer';
import ToDoChallenge from './pages/ToDoChallenge';

import { Container } from './styles/GlobalStyles';
import ConstructPage from './pages/ConstructPage';
import CallbackPage from './pages/CallbackPage';

const logged = false;

const LoggedRoute = ({ component: Component, ...rest }) => (

<Route
{...rest}
render={(props) =>
logged ? (
logged() ? (
<Component {...props} />
) : (
<Redirect
Expand All @@ -41,6 +43,7 @@ function Routes() {
<Container>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/callback" component={CallbackPage} />
<Route path="/challenges" exact component={Challenges} />
<Route path="/challenges/:id/details" component={Detail} />
<Route path="/devs" component={Devs} />
Expand All @@ -55,7 +58,6 @@ function Routes() {
component={MyChallenges}
/>
<LoggedRoute path="/dashboard" component={Dashboard} />
<Route path="/login" component={ConstructPage} />
</Switch>
</Container>
<Footer />
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/services/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const TOKEN_KEY = "@DevChallenge-Token";

export const isAuthenticated = () => localStorage.getItem(TOKEN_KEY) !== null;

export const login = token => {
localStorage.setItem(TOKEN_KEY, token);
};

export const logout = () => {
localStorage.removeItem(TOKEN_KEY);
};