Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function useNetlifyIdentity(domain, onAuthChange = () => {}) {
const authRef = React.useRef(
new GoTrue({
APIUrl: `${domain}/.netlify/identity`,
setCookie: true
})
);
const [user, setUser] = React.useState(null);
const _setUser = _user => {
setUser(_user);
onAuthChange(_user); // if someone's subscribed to auth changes, let 'em know
return _user; // so that we can continue chaining
};
/******* OPERATIONS */
// make sure the Registration preferences under Identity settings in your Netlify dashboard are set to Open.
const signupUser = (email, password) =>
authRef.current.signup(email, password).then(_setUser); // TODO: make setUser optional?
const loginUser = (email, password) =>
this.netlifyLargeMediaURL = getEndpoint(
config.getIn(['backend', 'large_media_url'], defaults.largeMedia),
netlifySiteURL,
);
const backendTypeRegex = /\/(github|gitlab|bitbucket)\/?$/;
const backendTypeMatches = this.gatewayUrl.match(backendTypeRegex);
if (backendTypeMatches) {
this.backendType = backendTypeMatches[1];
this.gatewayUrl = this.gatewayUrl.replace(backendTypeRegex, '');
} else {
this.backendType = null;
}
this.authClient = window.netlifyIdentity
? window.netlifyIdentity.gotrue
: new GoTrue({ APIUrl });
AuthenticationPage.authClient = this.authClient;
this.backend = null;
}