Created
March 5, 2020 02:31
-
-
Save donrestarone/8895a0137ba76373872929996d7ff6de to your computer and use it in GitHub Desktop.
sample login and logout with fetch and cookie based authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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