Skip to content

Commit 79507a7

Browse files
committed
Added frontend external auth
1 parent d8ff104 commit 79507a7

7 files changed

Lines changed: 92 additions & 3 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Join Auth via Yandex
2+
3+
Scenario: Auth
4+
Given I am a guest user
5+
And I am on "/join" page
6+
When I click "auth-external-yandex" element
7+
Then I see "Auth with Yandex"
8+
When I click "oauth-new" element
9+
Then I see "logout-button" element

frontend/src/Home/Home.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function Home(): React.JSX.Element {
2121
<Link to="/join" data-testid="join-link">
2222
Join
2323
</Link>
24-
<button type="button" data-testid="login-button" onClick={() => login()}>
24+
<button type="button" data-testid="login-button" onClick={() => login({})}>
2525
Log In
2626
</button>
2727
</>

frontend/src/Join/Join.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import System from '../Layout/System'
33
import { Link, Navigate } from 'react-router-dom'
44
import JoinForm from './JoinForm'
55
import { useAuth } from '../OAuth/Provider'
6+
import AuthExternal from '../OAuth/External/AuthExternal'
67

78
export default function Join(): React.JSX.Element {
89
const { isAuthenticated } = useAuth()
@@ -15,6 +16,7 @@ export default function Join(): React.JSX.Element {
1516
<System>
1617
<h1>Join to Us</h1>
1718
<JoinForm />
19+
<AuthExternal />
1820
<p>
1921
<Link to="/">Back to Home</Link>
2022
</p>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.external {
2+
margin: 1rem 0;
3+
padding: 1rem 0;
4+
border-top: 1px solid #eee;
5+
border-bottom: 1px solid #eee;
6+
}
7+
8+
.external .heading {
9+
font-size: 0.8rem;
10+
text-align: center;
11+
margin-bottom: 1rem;
12+
}
13+
14+
.external .items {
15+
display: flex;
16+
flex-direction: row;
17+
justify-content: center;
18+
}
19+
20+
.external .item {
21+
margin-right: 0.5rem;
22+
border-radius: 3px;
23+
padding: 0.6rem 1rem;
24+
cursor: pointer;
25+
border: 1px solid #ccc;
26+
background: linear-gradient(to bottom, #fff, #f6f6f6);
27+
color: #222;
28+
font-size: 0.8rem;
29+
line-height: 1;
30+
text-decoration: none;
31+
text-align: center;
32+
}
33+
34+
.external .item:last-of-type {
35+
margin-right: 0;
36+
}
37+
38+
.external .item.yandex {
39+
border: 1px solid #f60;
40+
background: linear-gradient(to bottom, #ff7600, #f00);
41+
color: #fff;
42+
}
43+
44+
.external .item.mailru {
45+
border: 1px solid #064fcd;
46+
background: linear-gradient(to bottom, #256ce7, #064fcd);
47+
color: #fff;
48+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import React from 'react'
2+
import styles from './AuthExternal.module.css'
3+
import { useAuth } from '../Provider'
4+
5+
export default function AuthExternal(): React.JSX.Element {
6+
const { login } = useAuth()
7+
8+
return (
9+
<div className={styles.external}>
10+
<div className={styles.heading}>Or log in via:</div>
11+
<div className={styles.items} data-testid="auth-external">
12+
<div
13+
className={styles.item + ' ' + styles.yandex}
14+
onClick={() => login({ provider: 'yandex' })}
15+
data-testid="auth-external-yandex"
16+
>
17+
Yandex
18+
</div>
19+
<div
20+
className={styles.item + ' ' + styles.mailru}
21+
onClick={() => login({ provider: 'mailru' })}
22+
data-testid="auth-external-mailru"
23+
>
24+
MailRu
25+
</div>
26+
</div>
27+
</div>
28+
)
29+
}

frontend/src/OAuth/Provider/AuthContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createContext } from 'react'
33
export type AuthContextValue = {
44
isAuthenticated: boolean
55
getToken(): Promise<string>
6-
login(): void
6+
login(extra: Record<string, string>): void
77
logout(): void
88
loading: boolean
99
error: string | null

frontend/src/OAuth/Provider/AuthProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export default function AuthProvider({
154154
return () => window.removeEventListener('storage', listener)
155155
}, [])
156156

157-
const login = useCallback(async () => {
157+
const login = useCallback(async (extra: Record<string, string>) => {
158158
const currentLocation = window.location.pathname
159159
const codeVerifier = generateCodeVerifier()
160160
const codeChallenge = await generateCodeChallenge(codeVerifier)
@@ -172,6 +172,7 @@ export default function AuthProvider({
172172
redirect_uri: window.location.origin + redirectPath,
173173
scope,
174174
state,
175+
...extra,
175176
})
176177

177178
window.location.assign(authorizeUrl + '?' + args)

0 commit comments

Comments
 (0)