UNPKG

react-with-sanctum

Version:

React package for easy authenticate with Laravel Sanctum

203 lines (195 loc) 7.83 kB
import { createContext, Component, createElement } from 'react'; import axios from 'axios'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } const SanctumContext = createContext({}); axios.defaults.withCredentials = true; const token = localStorage.getItem('access_token'); if (token) { axios.defaults.headers.common['Authorization'] = `Bearer ${token}`; } class Sanctum extends Component { constructor(props) { super(props); this.state = { user: null, authenticated: null }; this.getCSRF = this.getCSRF.bind(this); this.handleError = this.handleError.bind(this); this.getUserData = this.getUserData.bind(this); this.signIn = this.signIn.bind(this); this.signUp = this.signUp.bind(this); this.signOut = this.signOut.bind(this); this.forgotPassword = this.forgotPassword.bind(this); this.resetPassword = this.resetPassword.bind(this); this.setUser = this.setUser.bind(this); this.checkAuthentication = this.checkAuthentication.bind(this); } handleError(error) { if (error.response && error.response.status === 401) { this.setState({ user: false, authenticated: false }); return false; } else { return error; } } getCSRF() { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, csrfCookieRoute } = this.props.config; return yield axios .get(`${apiUrl}/${csrfCookieRoute}`) .then(() => { return true; }) .catch((error) => { return this.handleError(error); }); }); } getUserData() { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, userObjectRoute } = this.props.config; const token = localStorage.getItem('access_token'); return yield axios .get(`${apiUrl}/${userObjectRoute}`, { headers: { 'Authorization': `Bearer ${token}` } }) .then(({ data }) => { this.setState({ user: data, authenticated: true }); return data; }) .catch((error) => { return this.handleError(error); }); }); } checkAuthentication() { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, userObjectRoute } = this.props.config; return yield axios .get(`${apiUrl}/${userObjectRoute}`) .then(({ data }) => { this.setState({ user: data, authenticated: true }); return true; }) .catch((error) => { return this.handleError(error); }); }); } signIn(signInData) { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, signInRoute } = this.props.config; yield this.getCSRF; axios .post(`${apiUrl}/${signInRoute}`, signInData) .then(({ data }) => localStorage.setItem('access_token', data)) .catch((error) => { return this.handleError(error); }); return this.getUserData; }); } signUp(signUpData) { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, signUpRoute } = this.props.config; yield this.getCSRF; axios .post(`${apiUrl}/${signUpRoute}`, signUpData) .then(({ data }) => localStorage.setItem('access_token', data)) .catch((error) => { return this.handleError(error); }); return this.getUserData; }); } forgotPassword(forgotPasswordData) { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, forgotPasswordRoute } = this.props.config; return axios .post(`${apiUrl}/${forgotPasswordRoute}`, forgotPasswordData) .then(() => { return true; }) .catch((error) => { return this.handleError(error); }); }); } resetPassword(resetPasswordData) { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, resetPasswordRoute } = this.props.config; return axios .post(`${apiUrl}/${resetPasswordRoute}`, resetPasswordData) .then(() => { return true; }) .catch((error) => { return this.handleError(error); }); }); } signOut() { return __awaiter(this, void 0, void 0, function* () { const { apiUrl, signOutRoute } = this.props.config; return axios .post(`${apiUrl}/${signOutRoute}`) .then(() => { this.setState({ user: false, authenticated: false }); return true; }) .catch((error) => { return this.handleError(error); }); }); } setUser(user, authenticated = true) { this.setState({ user, authenticated }); } componentDidMount() { if (this.props.checkOnInit) { this.checkAuthentication(); } } render() { return createElement(SanctumContext.Provider, { value: { user: this.state.user, authenticated: this.state.authenticated, signIn: this.signIn, signUp: this.signUp, signOut: this.signOut, forgotPassword: this.forgotPassword, resetPassword: this.resetPassword, setUser: this.setUser, checkAuthentication: this.checkAuthentication } }, this.props.children || null); } } Sanctum.defaultProps = { checkOnInit: true }; export { Sanctum, SanctumContext }; //# sourceMappingURL=index.esm.js.map