npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

authenticatejs

v1.0.7

Published

AuthenticateJs helps simplify your authentication system using node js. You can easily login, register and logout your users with easy-to-call functions! Now, you do not need to deal with the hassle of cookies when authenticating your user. Use Authentica

Downloads

16

Readme

authenticatejs

Authenticatejs helps simplify your authentication system using node js. You can easily login, register and logout your users with just one line of code!

Installation

Install authenticatejs with npm

  npm i authenticatejs

Documentation

You must import the authenticatejs after installing the package:

const auth = require("authenticatejs");

Next, make a mongoose model that authenticatejs will use to register users:

mongoose.connect("mongodb://localhost:27017/authDB", {
    useNewUrlParser: true,
    useUnifiedTopology: true
})

const userSchema = new mongoose.Schema({
    email: String,
    password: String
})

const User = mongoose.model("User", userSchema);

It is critical to run the initialize function first. Else, the authenticatejs authentication system would fail. Make sure you pass your express app function as a parameter.

app = express();
auth.initialize(app);

Now to register a user after retrieving form data, you can use the register function.

app.post("/register", async (req, res) => {
    const register = await auth.register(User, req.body.email, req.body.password);
    if (register.success){
        res.send("Success!")
    } else{
        res.send("Could not register!");
    }
})

In the register function, the default field names that authenticatejs would use would be username and password. Since, our User model has the schema that uses the field name email instead of username, we can pass this

const register = await auth.register(User, req.body.email, req.body.password, "email", "password");

Now, the second last and last parameter we pass to the register function has the field names that authenticatejs must use for the User model

If you want the register function to save additional details such as name, age, gender when registering the user, you must first modify the schema of your mongoose model

const userSchema = new mongoose.Schema({
    email: String,
    password: String,
    name: String,
    age: Number,
    gender: Boolean
});

Now, you can pass the field names and the values to authenticatejs register function and it will save these details as well

const register = await auth.register(User, req.body.email, req.body.password, "email", "password", [["name", req.body.name], ["age", req.body.age], ["gender", req.body.gender]]);

If you want to login a user, you can use the login function. The first parameter to pass is the response you receive from a callback in an express route:

app.post("/login", async (req, res) => {
    const login = await auth.login(res, User, "secret", req.body.email, req.body.password, "email", "password");
    if (login.success){
        res.send("Logged in!");
    } else{
        res.send("Not logged in!");
    }
})

The string "secret" that you pass to the login function is used to sign json web tokens. Make sure it is stored as an environment variable for security purposes.

If you have a certain page that should be accessed only by users who are logged in, you can do this check by using the isLoggedIn function. For this function you must again pass the same secret string you passed for the login function:

app.get("/", (req, res) => {
    if (auth.isLoggedIn(req, "secret")){
        res.send("Hello User");
    } else{
        res.redirect("/login");
    }
})

In order to retrieve the email of our user, we can use the getUserData function:

app.get("/", (req, res) => {
    if (auth.isLoggedIn(req, "secret")){
        const username = auth.getUsername(req, "email", "secret");
        res.send(`Hello ${username}!`);
    }
})

The parameter we have passed in the getUserData function is the field name we used for the username (in our case, email)

You can make the user logout as well:

app.post("/logout", (req, res) => {
    auth.logout(res);
})

Here is a list of errors that can occur while using authenticatejs. You can use these names to show appropriate errors to the user:

| Function | Error | | ------------- |:-------------:| | Register | UserExists | | Login | IncorrectPassword | | Login | UserDoesNotExist |

You access the type of error that has occurred using the following code:

const login = await auth.login(res, User, req.body.email, req.body.password, "email", "password");
if (login.success){
    res.send("Logged in!");
} else{
    const errorMsg = login.msg;
    res.send(`The following error has occurred: ${errorMsg}`);
}

Authors