Giter Club home page Giter Club logo

ssw_login's Introduction

A login form with route control

The aim of this exercise is to implement a login form that controls the routing mechanism: only the user that successfully completes the login process has access to the inner pages.

The web service offers two routes:

  • the "login" route, that is the landing point of our web service,ΓΉ
  • the "main" route, that is the protected route, accessible only after a successful login

The operation is based on two components:

  • The "login" component, visible when authorization is pending
  • The "main" component, visible after successful login

The directory of the login components hosts also:

  • an "auth" service implementing an observable
  • the "login" guard that implement a blocking "onActivate" method

Step 0

At this stage, we have simply created a new project using the Angular CLI, by specifying that we want the routing configured in our template project.

You can load to stackblitz this project, and proceed updating it.

Step 1

Here we create all the features of our project:

  • two components: one for the login form, another for the (empty) main service
  • a guard that will open/close the route to the main component
  • a service that will operate asynchronously user I/O

You can do this using the Stackblitz IDE

Observe

Browse the new template files

Step 2

The app-routing.module.ts is populated with suitable routes. The guard is added to the main route.

The html of the root app component contains only a title and the router-outlet.

The guard is always false.

Observe

Visiting the URL with empty route points to the login page ("login works"), the same with a login route. The URL with the "main" route returns only the title (easy to configure an "Access Denied" component).

Step 3

The login form is added with a view and no function.

Observe

See the form and type login and password, with no effect

Step 4

Fake authentication (always true) and accessor method (isLogged) added to the authentication service.

The login component invokes the injectable authentication service.

Observe

The console traces the call from the login component to the authentication service

Step 5

The guard uses the isLogged accessor of the injected authenticator to control the access to the main component.

The login handler routes to the user to the main component if the isLogged accessor is true, otherwise dispays a popup alert.

Observe

After filling the credentials (any), clicking on the ok button visualizes the "main" component (and hides the login). Change the value of the return value of the fake authenticator to false, and see the popup appear.

Step 6

The main component contains a logout button, linked to a function that calls a logout function in the authentication service, which sets to false the isLogged variable. The same function routes the user back to the login route.

Also the routing to the main component is moved inside the authentication service.

Observe

Clicking the logout button returns to the login screen

Step 7

Activate the credentials check with a pair user/password (user="user", password="secret"), and bind the variables in the module to variables to application variables with the NgModel directive.

Observe

The login mechanism is fully functional. Enjoy...

Step 8

An event handler, like that in the login component, should operate asynchronously. For this, we introduce an asynchronous subscribe, mediated by an Observable that the authorization server returns to the event handler. So that, when the form waits for the user to digit the credentials, the form function is suspended waiting for the subscribe to unlock.

Observe

Nothing... The operation is exactly the same as before, but this is the right way to do that.

ssw_login's People

Contributors

mastrogeppetto avatar

Watchers

James Cloos avatar  avatar

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.