diff --git a/README.md b/README.md
index ca36236a6..107907ea1 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,14 @@
# graasp-compose
+
+## How to use the api
+
+- add following hosts using `sudo vim /etc/hosts` with
+ - `127.0.01 web.graasp.org`
+ - `128.178.242.202 api.graasp.org`
+- add in your `.env.local` file
+
+```
+REACT_APP_API_HOST=http://api.graasp.org:3111
+PORT=3111
+HOST=web.graasp.org
+```
diff --git a/src/api/authentication.js b/src/api/authentication.js
index 322c05b05..0c3b79881 100644
--- a/src/api/authentication.js
+++ b/src/api/authentication.js
@@ -1,26 +1,5 @@
import { API_HOST } from '../config/constants';
-// payload = {email}
-export const getOwnItems = async () => {
- const req = await fetch(`${API_HOST}/own`, {
- method: 'GET',
- credentials: 'include',
- headers: { 'Content-Type': 'application/json' },
- });
- console.log(req)
-};
-
-// payload = {email}
-export const createItem = async () => {
- const req = await fetch(`${API_HOST}/items`, {
- method: 'POST',
- credentials: 'include',
- body: JSON.stringify({name:'myitem'}),
- headers: { 'Content-Type': 'application/json' },
- });
- console.log(await req.json())
-};
-
// payload = {email}
export const signIn = async (payload) => {
const req = await fetch(`${API_HOST}/login`, {
@@ -31,6 +10,14 @@ export const signIn = async (payload) => {
return req.status === 204;
};
+export const signOut = async () => {
+ const req = await fetch(`${API_HOST}/logout`, {
+ credentials: 'include',
+ method: 'GET',
+ });
+ return req.status === 204;
+};
+
// payload = {name, mail}
export const register = async (payload) => {
const req = await fetch(`${API_HOST}/register`, {
diff --git a/src/api/item.js b/src/api/item.js
new file mode 100644
index 000000000..32af24cd4
--- /dev/null
+++ b/src/api/item.js
@@ -0,0 +1,28 @@
+import { API_HOST } from '../config/constants';
+
+// payload = {email}
+export const getOwnItems = async () => {
+ const req = await fetch(`${API_HOST}/own`, {
+ method: 'GET',
+ credentials: 'include',
+ headers: { 'Content-Type': 'application/json' },
+ });
+ // eslint-disable-next-line no-console
+ console.log(req);
+};
+
+// payload = {email}
+export const createItem = async () => {
+ const req = await fetch(`${API_HOST}/items`, {
+ method: 'POST',
+ credentials: 'include',
+ body: JSON.stringify({ name: 'myitem' }),
+ headers: { 'Content-Type': 'application/json' },
+ });
+ // eslint-disable-next-line no-console
+ console.log(await req.json());
+};
+
+// we need this function for navigation purposes: when you click on an item, you want to see its 'immediate' children
+// eslint-disable-next-line import/prefer-default-export
+export const fetchItemImmediateChildren = () => {};
diff --git a/src/components/App.js b/src/components/App.js
index b4fff3f85..fce98d791 100644
--- a/src/components/App.js
+++ b/src/components/App.js
@@ -42,6 +42,7 @@ function App() {
+
diff --git a/src/components/SignIn.js b/src/components/SignIn.js
index 1a92292f9..1e3326dec 100644
--- a/src/components/SignIn.js
+++ b/src/components/SignIn.js
@@ -1,6 +1,5 @@
import React, { Component } from 'react';
import PropTypes from 'prop-types';
-import Cookies from 'js-cookie'
import { withRouter } from 'react-router';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';
@@ -10,7 +9,8 @@ import Divider from '@material-ui/core/Divider';
import FormControl from '@material-ui/core/FormControl';
import { Alert } from '@material-ui/lab';
import { REGISTER_PATH } from '../config/paths';
-import { createItem, getOwnItems, signIn } from '../api/authentication';
+import { signIn, signOut } from '../api/authentication';
+import { isSignedIn } from '../utils/common';
const styles = (theme) => ({
fullScreen: {
@@ -47,23 +47,12 @@ class SignIn extends Component {
state = {
isSuccess: null,
email: '',
+ isAuthenticated: false,
};
- componentDidUpdate() {
- //this.isSignedIn()
- }
-
- async componentDidMount() {
- this.isSignedIn()
- console.log(await createItem())
- }
-
- async isSignedIn() {
- await fetch("http://ielsrv7.epfl.ch/auth?t=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI4N2M0OTQwZS1mNzY0LTRkODYtODExNC0xZWRkOWVmZjJlZTAiLCJpYXQiOjE2MDU3NzYxMTEsImV4cCI6MTYwNTc3NzkxMX0.g9Kb2hauJyv25nX-Y-vtFHwsCglKXueo_Y-C4IvoG-M", {
- credentials: "include",
-
- })
- // getOwnItems()
+ componentDidMount() {
+ const isAuthenticated = isSignedIn();
+ this.setState({ isAuthenticated });
}
handleOnRegister = () => {
@@ -79,6 +68,11 @@ class SignIn extends Component {
this.setState({ isSuccess });
};
+ signOut = async () => {
+ const isSuccess = await signOut();
+ this.setState({ isSuccess });
+ };
+
handleOnChange = (e) => {
this.setState({ email: e.target.value });
};
@@ -86,9 +80,7 @@ class SignIn extends Component {
renderMessage = () => {
const { isSuccess } = this.state;
if (isSuccess) {
- return (
- An email was sent with the login link!
- );
+ return Success! ;
}
// is not triggered for null (initial case)
if (isSuccess === false) {
@@ -97,16 +89,22 @@ class SignIn extends Component {
return null;
};
- render() {
- const { classes } = this.props;
- const { email } = this.state;
+ renderSignOutButton = () => {
+ return (
+ <>
+ You are already signed in.
+
+ Click here to sign out
+
+ >
+ );
+ };
+ renderSignInForm = () => {
+ const { email } = this.state;
+ const { classes } = this.props;
return (
-
- {this.renderMessage()}
-
- Sign In
-
+ <>
Not registered? Click here to register
+ >
+ );
+ };
+
+ render() {
+ const { classes } = this.props;
+ const { isAuthenticated } = this.state;
+
+ return (
+
+ {this.renderMessage()}
+
+ Sign In
+
+ {!isAuthenticated && this.renderSignInForm()}
+ {isAuthenticated && this.renderSignOutButton()}
);
}
diff --git a/src/utils/api.js b/src/utils/api.js
deleted file mode 100644
index 95611d7b8..000000000
--- a/src/utils/api.js
+++ /dev/null
@@ -1,3 +0,0 @@
-// we need this function for navigation purposes: when you click on an item, you want to see its 'immediate' children
-// eslint-disable-next-line import/prefer-default-export
-export const fetchItemImmediateChildren = () => {};
diff --git a/src/utils/common.js b/src/utils/common.js
new file mode 100644
index 000000000..4e6e12686
--- /dev/null
+++ b/src/utils/common.js
@@ -0,0 +1,9 @@
+import Cookies from 'js-cookie';
+
+// could use redux store to keep session
+// todo: better check on session
+// eslint-disable-next-line import/prefer-default-export
+export const isSignedIn = () => {
+ const value = Cookies.get('session');
+ return Boolean(value);
+};