diff --git a/client/src/backendCalls/auth/registerUser.js b/client/src/backendCalls/auth/registerUser.js new file mode 100644 index 0000000..a1ace0e --- /dev/null +++ b/client/src/backendCalls/auth/registerUser.js @@ -0,0 +1,53 @@ +/** + * Registers a user with the given email, password, first and last name. + * If either first_name or last_name are undefined, the user will be registered + * without a first or last name respectively. + */ + +import store from '../../store.js'; + +export async function registerUser(email, password, first_name, last_name) { + var details = { + 'email': email, + 'password': password, + 'first_name': first_name, + 'last_name': last_name + } + + var formBody = []; + for (var property in details) { + if (details[property] === undefined) { + continue; + } + var encodedKey = encodeURIComponent(property); + var encodedValue = encodeURIComponent(details[property]); + formBody.push(encodedKey + '=' + encodedValue); + } + formBody = formBody.join('&'); + + const response = await fetch(store.authApiBase + '/user', { + method: 'POST', + mode: 'cors', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: formBody, + }); + + const body = await response.json(); + + console.log(body); + if (response.status !== 201) { + if (body.error) { + return body.error; + } + else { + return JSON.stringify(body); + } + } else { + return 200; + } +} + +export default registerUser diff --git a/client/src/backendCalls/auth/userLogin.js b/client/src/backendCalls/auth/userLogin.js new file mode 100644 index 0000000..02fcfab --- /dev/null +++ b/client/src/backendCalls/auth/userLogin.js @@ -0,0 +1,45 @@ +import store from '../../store.js'; + +export async function userLogin(email, password) { + var details = { + 'email': email, + 'password': password, + } + + var formBody = []; + for (var property in details) { + if (details[property] === undefined) { + continue; + } + var encodedKey = encodeURIComponent(property); + var encodedValue = encodeURIComponent(details[property]); + formBody.push(encodedKey + '=' + encodedValue); + } + formBody = formBody.join('&'); + + const response = await fetch(store.authApiBase + '/user/login', { + method: 'POST', + mode: 'cors', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: formBody, + }); + + const body = await response.json(); + + console.log(body); + if (response.status !== 201) { + if (body.error) { + return body.error; + } + else { + document.cookie = body.token; + return JSON.stringify(body); + } + } else { + return 200; + } +} + +export default userLogin diff --git a/client/src/store.js b/client/src/store.js index 8c96e86..5c45318 100644 --- a/client/src/store.js +++ b/client/src/store.js @@ -1,5 +1,6 @@ const store = { searchResults: [], apiBase: 'https://internado.ubclaunchpad.com', + authApiBase: 'http://localhost:5050' }; export default store; diff --git a/client/src/store.js.orig b/client/src/store.js.orig new file mode 100644 index 0000000..ed7ebab --- /dev/null +++ b/client/src/store.js.orig @@ -0,0 +1,12 @@ +const store = { + searchResults: [], +<<<<<<< HEAD + apiBase: 'http://localhost:5000', + authApiBase: 'http://localhost:5050' +||||||| merged common ancestors + apiBase: 'http://localhost:5000' +======= + apiBase: 'https://internado.ubclaunchpad.com', +>>>>>>> develop +}; +export default store; diff --git a/client/src/views/Login.js b/client/src/views/Login.js index a62c85b..abed97c 100644 --- a/client/src/views/Login.js +++ b/client/src/views/Login.js @@ -1,35 +1,81 @@ -import React from 'react'; +import React, {Component} from 'react'; import '../sass/LoginSignup.scss'; import { Button, Form, Grid, Header, Message, Segment } from 'semantic-ui-react'; +import userLogin from '../backendCalls/auth/userLogin.js'; +class LoginForm extends Component { + constructor(props) { + super(props); + this.state = { + formData: new Map([ + ['email', ''], + ['password', ''], + ]), + result: '' + }; + } -const LoginForm = () => ( -
- - -
- Log in -
-
- - - - - -
- - Don't have an Account? Sign Up - -
-
-
-) + handleDataChange = (e, item) => { + let newData = this.state.formData; + newData[item] = e.target.value; + this.setState({formData: newData}); + }; -export default LoginForm; \ No newline at end of file + handleSubmit = async(e) => { + let data = this.state.formData; + let response = await userLogin(data['email'], data['password']); + let responseHTML = ''; + try { + // If the login failed, the next line will fail to parse response + JSON.parse(response); + responseHTML = 'Logged in successfully'; + } catch { + // If the login failed, response will be a string describing the failure + responseHTML = {response} ; + } + this.setState({result: responseHTML}); + }; + + render() { + return ( +
+ + +
+ Log in +
+
+ + this.handleDataChange(e, 'email')} + /> + this.handleDataChange(e, 'password')} + /> + + +
+

+ {this.state.result} +

+ + Don't have an Account? Sign Up + +
+
+
+ ) + } +} + +export default LoginForm; diff --git a/client/src/views/SignUp.js b/client/src/views/SignUp.js index 6954a40..f3273e9 100644 --- a/client/src/views/SignUp.js +++ b/client/src/views/SignUp.js @@ -1,43 +1,105 @@ -import React from 'react'; +import React, {Component} from 'react'; import '../sass/LoginSignup.scss'; import { Button, Form, Grid, Header, Message, Segment } from 'semantic-ui-react'; +import registerUser from '../backendCalls/auth/registerUser.js'; -const SignUpForm = () => ( -
- - -
- Sign up -
-
- - - - - - - -
- - Already a member? Log in - -
-
-
-) +class SignUpForm extends Component { + constructor(props) { + super(props); + this.state = { + formData: new Map([ + ['username', ''], + ['email', ''], + ['password', ''], + ['repeatPassword', ''] + ]), + result: '' + }; + } -export default SignUpForm; \ No newline at end of file + handleDataChange = (e, item) => { + let newData = this.state.formData; + newData[item] = e.target.value; + this.setState({formData: newData}); + }; + + handleSubmit = async(e) => { + let data = this.state.formData; + if (data['password'] !== data['repeatPassword']) { + this.setState({result: Passwords do not match}) + + } else { + let response = await registerUser(data['email'], data['password'], data['username'], undefined); + let responseHTML = ''; + if (response === 200) { + responseHTML = 'Account successfully created'; + } else { + responseHTML = {response} ; + } + this.setState({result: responseHTML}); + } + }; + + render() { + return ( +
+ + +
+ Sign up +
+
+ + this.handleDataChange(e, 'username')} + /> + this.handleDataChange(e, 'email')} + /> + this.handleDataChange(e, 'password')} + /> + this.handleDataChange(e, 'repeatPassword')} + /> + + +
+

+ {this.state.result} +

+ + Already a member? Log in + +
+
+
+ ) + } +} + +export default SignUpForm;