Git Product home page Git Product logo

adonis-starter's Introduction

MVP starter

A Node.js, TypeScript & postgres SQL starter built upon Adonis JS framework, with ready-to-use users management system, to focus on building your new ideas.

🔋 Batteries included:

  • Sign up form
  • Sign in form
  • Logout
  • List / create / delete users in admin page
  • Forgot password
  • Email verification
  • Roles and permissions with AdonisJS bouncers

Installation

Requirements:

  • ⚠️ Latest release of Node.js 14, along with npm >= 6.0.0.
  • A Postgres database.
  • An STMP server to send emails (you might use, for example, Mailgun, Sparkpot, Amazon SES)
# Check your node version
node -v
# Check your npm version
npm -v

# Clone the repo
# stable branch, to start a new project
git clone -b main [email protected]:yann-yinn/adonis-starter.git
# If you want to contribute: clone dev branch instead of main.
# git clone -b dev [email protected]:yann-yinn/mvp-starter.git

# Install dependencies
npm install

# Configure your environment variables:
#
# 1) Generate your unique app id, you need this value for APP_KEY env var in .env.
node ace generate:key
# 2) copy env.example file to create a ".env" file
# edit the env file and set required env vars.
cp env.example .env

# Create postgres tables
 npm run migrate-up

# Launch dev server !
npm run dev

Contribute

Fork dev branch and make a PR againts the dev branch.

Roles and Permissions

Adding new role

You can add new roles inside config/roles.ts file. By default, there is only "root", "admin" and "member" roles. Root role is special and MUST NOT be deleted. First created user became automatically a "root" user.

import { Role } from "App/types";

const roles: Role[] = [
  // root is a special role and has all authorizations.
  {
    id: "root",
    label: "Root",
  },
  // Member is the default role when someone creates a new account.
  {
    id: "member",
    label: "Member",
  },
  // Admin can create / delete users, except the root user.
  {
    id: "admin",
    label: "Administrator",
  },
];
export default roles;

Definining authorizations

MVP starter is using "bouncers" from Adonis JS framework to define authorizations.

See start/bouncer.ts File for predefined authorizations or to add new authorizations.

Example bouncer: "Admin role can edit any post. Member can only edit their own posts":

.define("editPost", (user: User, post: Post) => {
  if (userHasRoles(["admin"], user)) {
    return true;
  }
  if (userHasRoles(["member"], user) && user.id === post.userId) {
    return true;
  }
  return false;
})

Then, in your controller, use the defined bouncer like so (don't forget the await keyword!)

public async edit({ view, request, bouncer }: HttpContextContract) {
  const entity = await this.entityModel.findOrFail(request.param("id"));
  await bouncer.authorize("editPost", entity);
  // etc
}

You can control authorizations in the templates too:

@can('editPost', entity)
  <a href="{{entity._editLink}}">Edit</a> </td>
@end

See adonis docs on "bouncers" for more details: https://docs.adonisjs.com/guides/authorization

FAQ

POSTGRES SSL AND HEROKU

Fix SSL issue in development with postgres hosted with Heroku: configure rejectUnauthorized in your config/database.ts config file.

// config/database.ts
connections: {
  pg: {
    client: "pg",
    connection: {
      ssl: {
        rejectUnauthorized: Env.get("NODE_ENV") === "production" ? true : false,
      },
      // ...

Changelog

  • 5 oct. User is (optionnaly) blocked until his email is verified (thanks @Yoann-TYT)
  • 5 oct. Add "root" role, (default role for the very first registered user)
  • 5 otc. Add password validations rules
  • 5 oct. Show / Hide password (thanks @Sreejit7)
  • 5 oct. When user upload a new profile picture, delete the old one (thanks @Yoann-TYT)
  • nov. forgot password and mail verification (thanks @HugoLd)

adonis-starter's People

Contributors

hugold avatar sreejit7 avatar yann-yinn avatar yoann-tyt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

adonis-starter's Issues

Refacto passworfields to a component

Password field is becoming more and more complex, leading to duplicate code in several different forms.

  • confirmation field
  • icon to show / hide password
  • instructions for the password
  • strength meter is coming soon.

We should extract it to a adonis "component", and the use this component in all form using the password fields. (see this comment for more details on the component #18 (comment))

Envoi des emails : proposition

Voir doc Adonis : https://docs.adonisjs.com/guides/mailer

1) Email à la création du compte

A la création d'un nouveau compte utilisateur, envoyer un email qui dit.

Welcome {{user.name}}
please click on the following link to enable your account
{{activationLink

Dans la base de données

Dans la base de données (créer une nouvelle migration pour les users qui alter la table)

  • email_verified (indique que la personne a bien activer son email) string 255 chars
  • blocked: boolean: true si utilisateur bloqué (avant confirmation email par exemple), false si utilisateur actif (après activation email)

sur App/Model/User: ajouter propriétés emailVerified et blocked

2) Email Mot de passe oublié

( #6 )

Un lien "mot de passe oublié" sur le formulaire de login envoie un email à l'utilisateur.

Celui lui envoie un mail avec lien qui lui permet de remettre à jour son mot de passe

User blocked until email is verified

config/starter.ts file: if blockUserUntilEmailVerification is true,

  1. user should not be able to login

  2. A flash message is displayed :

const message = "You must confirm your email, check your inbox. Click here to resend an email verification for your account".
session.flash({notification: message})
  1. on click, a page with a form offers him to re-enter its email and resend verification link.

Mot de passe oublié

Créer le formulaire qui permet de mettre à jour son mot de passe oublié

  • ajout du lien "forgot password" sur le formulaire de login
  • accès à la page de reset du password depuis le lien (reçu théoriquement via email, les emails n'étant pas encore gérés)

Mitigate brute force attacks

There is no mechanism for now to prevent a brute force attack (submitting a lot of posts request to login to the app).

Maybe allow only 3 or 5 login within 24 hours attempts is good enough for now ?

Create an online demo

Don't want to pay for a demo, where could we host postgres + node for free to create an online Demo ?
Heroku free plan ? Explore solutions

Problem with a timed logout

Hi.

First, thank you for this starter, it is very easy and straightforward to start a new project.
My problem is with timed logout. Each time when the app logs me out, I'm presented with the login form which for some reason is not working. After submitting all credentials the page starts to load endlessly. The fix is quite simple, just refresh the login page before entering the credentials or while it's endlessly loading and repeat login submission.

Here is my screenshot of the terminal with the visible error message:
Slika zaslona s 2022-05-26 16-30-42

I'm using elementaryOS 6.1 Linux, an Ubuntu 20.04-based distro.

Thank you again for your time and effort in putting this gem together.

Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.