Service: OpenAuth Password - EyevinnOSC/community GitHub Wiki

Getting Started

In this tutorial we will show how easy you can add user registration and authentication to your application using open web services in Eyevinn Open Source Cloud.

Prerequisites

Step 1: Create the user database

Login and navigate to the CouchDB service in Eyevinn Open Source Cloud web console. Create a new couchdb instance.

Create CouchDB

Click on the instance card and open the web user interface for the database. In the user interface create a database that we can call users.

User database

Note down the URL to the database instance, in our example it is https://eyevinnlab-blog.apache-couchdb.auto.prod.osaas.io.

Step 2 (optional): Setup a fake SMTP mailer

For the purpose of you to be able to follow this example without having access to a real SMTP server for outgoing email you can create a fake SMTP mailer at Ethereal Email.

Create an account and note down the SMTP settings generated.

Fake SMTP mailer

Step 3: Launch password authentication service

Now navigate to the OpenAuth Password open web service and press "Create authservice".

Enter the following in the create dialog:

  • Name: blog
  • UserDbUrl: https://<db-user>:<db-password>@<db-url>/<db-name> where <db-url> is the hostname for the database instance you created and <db-name> the name of the database you created. In this example this is https://admin:[email protected]/users
  • SmtpMailerUrl: smtp://<smtp-user>:<smtp-password>@<smtp-host>:<smtp-port>. In our example with a fake SMTP mailer we have smtp://[email protected]:[email protected]:587

Create password auth service

Note down the URL to the password authentication service available on the instance card. In our example it is https://eyevinnlab-blog.eyevinn-openauth-pwd.auto.prod.osaas.io.

Step 4: Develop a simple NextJS application

Now we will develop a simple NextJS application to verify that it works. It is based on the example application found in the OpenAuth GitHub repository.

Clone the OpenAuth repository.

% git clone [email protected]:openauthjs/openauth.git 

Create a project folder for your application and copy the example code from the OpenAuth repository.

% mkdir my-app
% cd my-app
% cp -r ../openauth/examples/client/nextjs/* .

As we copied this example from a mono repo we need to uninstall the referenced openauth library and install the published package instead.

% npm uninstall @openauthjs/openauth 
% npm install --save @openauthjs/openauth

Install the other dependencies.

% npm install

Open the file app/auth.ts and replace the following in the file

export { subjects } from "../../../subjects"

with

import { createSubjects } from "@openauthjs/openauth/subject"
import { object, string } from 'valibot';

export const subjects = createSubjects({
  user: object({
    userId: string()
  })
});

Open the file app/page.tsx and change the following line:

  Logged in as <code>{subject.properties.id}</code>.

to

  Logged in as <code>{subject.properties.userId}</code>.

Then change the issuer URL http://localhost:3000 to the URL to the password authentication service you created, as in this example:

export const client = createClient({
  clientID: "nextjs",
  issuer: "https://eyevinnlab-blog.eyevinn-openauth-pwd.auto.prod.osaas.io",
})

Install the valibot dependency.

% npm install --save valibot

Now we can run the development server.

% npm run dev

Open your web browser and go to http://localhost:3000 and you will see the following.

NextJS auth example

Click on the Login with OpenAuth button.

Create a new account by clicking on the link Register.

Register account

Enter an email and password.

Entered email and password

Go your inbox in the fake SMTP mailer (or a real one if you are using a real emailer).

Fake emailer

Enter the code you received in the email.

Entered code

A new user has now been created and we can verify that it is in our database.

User entered in database

And you are also logged in to the application.

Registered user

Now we can press Logout and verify that we can login with the password we created.

Login

And we are back in!

Logged in yes

We can also verify that we can handle users that forgot their password. Press link Forgot password and enter the email.

Enter email

Check the inbox for the code and enter it.

Entered code

⚠️ **GitHub.com Fallback** ⚠️