1.1.6 • Published 3 years ago
my-auth-library v1.1.6
Installation
With NPM:
npm install my-auth-library
With Yarn:
yarn add my-auth-library
Usage
Instantiate Auth
Object
In your server side code, import and instantiate the Auth
object:
import Auth, { config, baseApiURL } from "my-auth-library";
// In NextJS
import getConfig from "next/config";
const { serverRuntimeConfig } = getConfig();
const secret = serverRuntimeConfig.secret;
// In ReactJS
// const secret = process.env.SECRET
export { auth };
// Set your API endpoint
config("http://localhost:3000/api");
const auth = new Auth("http://localhost:3000/api", secret, "/api/users/authenticate");
Auth Parameters
API root endpoint: String
Secret: String
Authentication endpoint: String
Use userService
Utility Methods to Authenticate
import { userService } from "my-auth-library/dist/services";
// In NextJS
import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();
const loginApiUrl = `${publicRuntimeConfig.apiUrl}/users/authenticate`;
// In ReactJS
// const loginApiUrl = process.env.LOGIN_API_URL
const redirect_on_sign_in_url = "/";
function onSubmit({ username, password }: { [x: string]: string }) {
return userService
.login(username, password, loginApiUrl)
.then(() => {
// get return url from query parameters or default to '/'
let returnUrl = redirect_on_sign_in_url || "/";
router.push({ pathname: returnUrl });
})
.catch((error: string) => {
console.log(error);
});
}