If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

This is a brief explanation based on this post in Kolaborativa's blog.

There are two different ways to make an single login with two applications: CAS (Central Authentication Service) and Database Cooperation. CAS will let you use the database and login/register/etc forms of a main application, while the Database Cooperation will let you use each application login/register/etc forms but all with the same database.

I will call the main application Main and the secundary application Other.


To use CAS:

On your db.py model file of Other application change this line:

auth = Auth(db)

to this line:

auth = Auth(db,cas_provider = 'http://applocal/main/default/user/cas')

What you're doing here is, simply, say where the user should make register/login/etc. The URL specify the applocal (localhost or your domain), the Main application, the default controller, the user function, and CAS.


Well, that's it. So simple like is everything working.


To use Database Cooperation:

On your db.py of Main application create a new database. Why? Well, if we are going to share the data of one database with another, maybe some of the tables are useless for one application and worth for another, and we don't want this first one to mess with the second. So we are going to isolate the Auth tables on an specific database. It will go like this:

db = DAL('sqlite://storage.sqlite',pool_size=1,check_reserved=['all'])
#right after the application existing database:
auth_db = DAL('sqlite://users.sqlite')

Now, in the same file, change this line:

auth = Auth(db)

to this line:

auth = Auth(auth_db)
#this way we are going to use the Auth tables on our new database

Now, on the db.py file of the Other application, create a conection for the same database of the Main application.

auth_db = DAL('sqlite://users.sqlite', folder='/home/yourname/web2py/applications/main/databases/')

This will tell on what location your database is. Replace the folder content with the appropriate database location and don't forget to use the same database name that you just have passed to the Main application.

If you use an external database, you can just use the same conection string on both databases:

auth_db = DAL('mongodb://username:password@server.mongohq.com:10043/mydb')

Don't forget to change this line:

auth = Auth(db)

to this line:

auth = Auth(auth_db)

Now everything is done. Congratulations, your users can use a single login on all your applications!


PS: this will not make the user to continue logged if he change between applications. To do this follow this slice steps.

Related slices

Comments (0)

Hosting graciously provided by:
Python Anywhere