Skip to content

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 AuthenticateJs now!

VOYAGERX013/authenticatejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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 AuthenticateJs now!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published