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 = () => ( -
+ {this.state.result} +
++ {this.state.result} +
+