Skip to content

Instantly share code, notes, and snippets.

@donrestarone
Created March 5, 2020 02:31
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save donrestarone/8895a0137ba76373872929996d7ff6de to your computer and use it in GitHub Desktop.
sample login and logout with fetch and cookie based authentication
export const login = (email, password) => {
return new Promise((resolve, reject) => {
let endpoint = `http://api.your-domain-here.ngrok.io/api/core/v1/sessions`;
fetch(endpoint, {
method: "POST",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
body: JSON.stringify({
email: email,
password: password,
})
})
.then(e => {
if (e.ok) {
resolve(e);
} else {
reject(e);
}
})
.catch(e => console.log("error::", e));
});
};
export const logOut = (userId) => {
return new Promise((resolve, reject) => {
let endpoint = `http://api.your-domain-here.ngrok.io/api/core/v1/sessions/${userId}`;
fetch(endpoint, {
method: "DELETE",
credentials: "include",
headers: {
"Content-Type": "application/json",
"Accept": "application/json",
},
})
.then(e => {
if (e.ok) {
resolve(e);
} else {
reject(e);
}
})
.catch(e => console.log("error::", e));
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment