Skip to content

Commit d2ca1b8

Browse files
committed
Added initial client authentication functionality
1 parent cabfcde commit d2ca1b8

File tree

5 files changed

+91
-35
lines changed

5 files changed

+91
-35
lines changed

src/Client/ClientAuthentication.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,46 @@ export default class ClientAuthentication extends EventTarget implements IClient
1616

1717
async signIn(username: string, password: string): Promise<boolean> {
1818
this._lastError = null;
19-
const response = await this._client.axios.post('auth/login', {
20-
username: username,
21-
password: password
22-
});
23-
if (response.status == 201) {
24-
window.localStorage.setItem(ClientAuthentication.TOKEN_STORAGE_KEY, response.data.token);
25-
return true;
19+
try {
20+
const response = await this._client.axios.post('auth/login', {
21+
username: username,
22+
password: password
23+
});
24+
if (response.status == 201) {
25+
window.localStorage.setItem(ClientAuthentication.TOKEN_STORAGE_KEY, response.data.token);
26+
return true;
27+
}
28+
if (response.data.error) {
29+
this._lastError = response.data.error;
30+
}
31+
return false;
2632
}
27-
if (response.data.error) {
28-
this._lastError = response.data.error;
33+
catch (err: any) {
34+
this._lastError = err;
35+
return false;
2936
}
30-
return false;
3137
}
3238

3339
async signUp(username: string, password: string): Promise<boolean> {
3440
this._lastError = null;
35-
const response = await this._client.axios.post('auth/register', {
36-
username: username,
37-
password: password
38-
});
39-
if (response.status == 201) {
40-
window.localStorage.setItem(ClientAuthentication.TOKEN_STORAGE_KEY, response.data.token);
41-
return true;
41+
try {
42+
const response = await this._client.axios.post('auth/register', {
43+
username: username,
44+
password: password
45+
});
46+
if (response.status == 201) {
47+
window.localStorage.setItem(ClientAuthentication.TOKEN_STORAGE_KEY, response.data.token);
48+
return true;
49+
}
50+
if (response.data.error) {
51+
this._lastError = response.data.error;
52+
}
53+
return false;
4254
}
43-
if (response.data.error) {
44-
this._lastError = response.data.error;
55+
catch (err: any) {
56+
this._lastError = err;
57+
return false;
4558
}
46-
return false;
4759
}
4860

4961
}

src/Client/IClientAuthentication.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default interface IClientAuthentication {
2+
lastError: string;
23
signIn(username: string, password: string): Promise<boolean>;
34
signUp(username: string, password: string): Promise<boolean>;
45
}

src/React/Layout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default class Layout extends React.Component<PropsWithChildren<Props>> {
1010

1111
render() {
1212
return (
13-
<div>
13+
<div className='Layout'>
1414
{this.props.children}
1515
</div>
1616
);

src/React/Page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Page extends React.Component<PropsWithChildren<Props>> {
1111

1212
render() {
1313
return (
14-
<div>
14+
<div className='Page'>
1515
{this.props.children}
1616
</div>
1717
);

src/React/SignIn/SignIn.tsx

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,87 @@ type Props = {
66
};
77

88
type State = {
9-
signIn: boolean
9+
signIn: boolean;
10+
errorMessage: string;
1011
};
1112

1213
export default class SignIn extends React.Component<PropsWithChildren<Props>, State> {
14+
private emailRef: React.RefObject<HTMLInputElement> = React.createRef();
15+
private password1Ref: React.RefObject<HTMLInputElement> = React.createRef();
16+
private password2Ref: React.RefObject<HTMLInputElement> = React.createRef();
17+
private errorMessageRef: React.RefObject<HTMLDivElement> = React.createRef();
18+
19+
1320
constructor(props: Props) {
1421
super(props);
1522
this.handleSignInClick = this.handleSignInClick.bind(this);
1623
this.handleSignUpClick = this.handleSignUpClick.bind(this);
17-
this.state = { signIn: true };
24+
this.state = {
25+
signIn: true,
26+
errorMessage: null
27+
};
1828
}
1929

20-
handleSignInClick() {
21-
30+
async handleSignInClick() {
31+
if (this.state.signIn) {
32+
if (await this.props.auth.signIn(this.emailRef.current.value, this.password1Ref.current.value)) {
33+
this.setState({ errorMessage: null });
34+
}
35+
else {
36+
this.setState({ errorMessage: 'Error signing in: ' + this.props.auth.lastError });
37+
}
38+
}
39+
else {
40+
this.setState({ signIn: true });
41+
}
2242
}
2343

24-
handleSignUpClick(){
44+
async handleSignUpClick(){
45+
if (this.state.signIn) {
46+
this.setState({ signIn: false });
47+
}
48+
else {
49+
if (this.password1Ref.current.value != this.password2Ref.current.value) {
50+
this.setState({ errorMessage: 'Passwords do not match'});
51+
return;
52+
}
2553

54+
if (await this.props.auth.signUp(this.emailRef.current.value, this.password1Ref.current.value)) {
55+
this.setState({ errorMessage: null });
56+
}
57+
else {
58+
this.setState({ errorMessage: 'Error signing up: ' + this.props.auth.lastError });
59+
}
60+
}
2661
}
2762

2863
render() {
2964
return (
30-
<div>
65+
<div className="SignIn">
3166
Sign In
3267
<hr></hr>
3368
<div>
34-
Username: <input type="email" id="SignInUsername"></input>
69+
Username: <input type="email" id="SignInUsername" ref={this.emailRef}></input>
3570
</div>
3671
<div>
37-
Password: <input type="password" id="SignInPassword1"></input>
72+
Password: <input type="password" id="SignInPassword1" ref={this.password1Ref}></input>
3873
</div>
39-
{this.state.signIn ? <div></div> :
74+
{this.state.signIn ?
75+
<div></div> :
4076
<div>
41-
Password: <input type="password" id="SignInPassword2"></input>
42-
</div>}
77+
Password: <input type="password" id="SignInPassword2" ref={this.password2Ref}></input>
78+
</div>
79+
}
4380
<div>
44-
<input type="button" value="Sign In" onClick={this.handleSignInClick}></input>
45-
<input type="Button" value="Sign Up" onClick={this.handleSignUpClick}></input>
81+
<input type="button" value="Sign In" onClick={this.handleSignInClick} readOnly></input>
82+
<input type="Button" value="Sign Up" onClick={this.handleSignUpClick} readOnly></input>
4683
</div>
84+
{!this.state.errorMessage ?
85+
<div></div> :
86+
<div id="errorMessage" ref={this.errorMessageRef}>
87+
{this.state.errorMessage}
88+
</div>
89+
}
4790
</div>
4891
);
4992
}

0 commit comments

Comments
 (0)