gocodebox / lifterlms-rest

LifterLMS REST API Feature Plugin
6 stars 7 forks source link

Login and Registration API #223

Closed oleksandr-rapiy closed 3 years ago

oleksandr-rapiy commented 3 years ago

Hello everyone, does LifterLMS provide API to login student using other services? For example, I wanna login new student to system, which already use LifterLMS plugin, but through my own web site.

How can I do that ?

thomasplevy commented 3 years ago

@OleksandrRapiy

Login via REST

Our REST api does not (and likely will not) provide the ability to login users via REST calls.

This gets complicated but (while it is debatable) user interaction like logging in and out does not qualify as a semantically valid REST interaction. Our REST API is built out of resources (courses, memberships, users, enrollments, etc...) these are all nouns. Logging a user into a website is an action (a verb) and does not qualify a technically valid REST resource (again, this is debatable, but given our goals and philosophies on our API this is our reasoning).

LifterLMS itself relies on the WordPress core for end-user authentication on the WP website. In order to login a user a session must be created (relying on cookies). We could create a /session endpoint which would allow for the creation of a user session through the API but, while possible, just isn't something we aim to tackle through our API.

But! If you're using LifterLMS you're using WordPress and you can quite easily find plugins that enable this functionality that you're looking for.

LifterLMS users (students, instructors, etc...) are all WordPress users. They can all be managed through WP core REST endpoints. They can all be logged into a site using WP core methods. Should you wish to log them in via REST endpoints, all you need to do is find a plugin that handles end user authentication via the REST API. Off the top of my head I don't have a recommendation for you but I'm certain something like this exists.

User Registration

LifterLMS-specific user roles can be created (registered) via the LifterLMS REST API. See our documentation for details on how to do this:

oleksandr-rapiy commented 3 years ago

@thomasplevy
Great, Thank you so much

oleksandr-rapiy commented 3 years ago

@thomasplevy Okey thank you again, but can you help me a little bit.

Can I get some courses by student auth cookies ? Or how can I get information about student account and so on, by student auth cookies, for example by wp_llms_session cookie ?

thomasplevy commented 3 years ago

Can I get some courses by student auth cookies ?

The authentication cookies (set by the WordPress core) are used to determine the currently logged in user. It's best to use WP core methods to identify the current user -- as opposed to reading information from the cookie directly. If you're looking to use the REST API you could use the following endpoint to determine the current user:

GET /wp/v2/users/me: https://developer.wordpress.org/rest-api/reference/users/#definition-example-request-2

Our API does not currently have an endpoint like this, but we'll likely add one in the future, see #183

Or how can I get information about student account and so on, by student auth cookies, for example by wp_llms_session cookie ?

This particular cookie isn't used to authenticate, instead it is used to track an authenticated users activity in various areas of the LifterLMS software.

There's no real reason to access this directly and we don't have any REST interactions with this session cookie. However, if you do need to access it programmatically you can do so by using the LLMS_Session object.

Instead of constructing a new object, obtain an instance of the current session from the main LifterLMS class instance: https://github.com/gocodebox/lifterlms/blob/00e669cfdc3aec1446a23db9c46ec19b345835f9/class-lifterlms.php#L57-L62

eg: $session = llms()->session;

thomasplevy commented 3 years ago

@OleksandrRapiy any other questions let us know, otherwise I'll consider this resolved

If you need more help respond and reopen!

oleksandr-rapiy commented 3 years ago

@thomasplevy Thank you so much for your answers. I have one question for you. How can I get order history? For example list of courses what student buy?

thomasplevy commented 3 years ago

@OleksandrRapiy

How can I get order history?

We have not yet implemented orders as a resource, though we intend to. This is one of the resources slated for "Phase 2" of our API development. You can see what else is in phase 2 here: https://github.com/gocodebox/lifterlms-rest/issues/1

I don't have an ETA for any of this, it'll probably be rolled out little by little. E-commerce resources will probably be grouped together into a sub phase (Orders, Transactions, Coupons, Refunds, etc...)

For example list of courses what student buy?

You can pull a list of a student's enrollment data using this endpoint:

GET /students/{id}/enrollments: https://developer.lifterlms.com/rest-api/#tag/Student-Enrollments/paths/~1students~1{id}~1enrollments/get

oleksandr-rapiy commented 3 years ago

@thomasplevy
Ok thank you, I got it.