Authentication
How the WPLMS Authentication works.
The Version 4 comes with a JWT login system which means the authentication happens in a browser. Which means that when a page is loaded the WordPress is not aware of which user is accessing the page. Now, this means that the server will always render the page as a static and you will be able to host your sites 100% cached, or even static HTML pages.
The benefit of using JWT tokens is huge both in terms of Loading speed and scalability. The WPLMS can work as a part of your site without impacting the overall site performance. That is "Do not convert your entire site into an LMS instead chose only a specific part of your site to work as LMS". This is only possible if we migrate away from the WordPress cookie based authentication structure to a JWT based login system.

Generating the Token

The VibeBP plugin uses the function : vibebp_generate_token($user) for generating the token for the user. The $user is the a user object see below
1
{
2
'id' => //ID of the WordPress user
3
'username'=> // Login username of WordPress User
4
'slug'=> // User nicename of WordPress User
5
'email'=>// User email of WordPress User
6
'avatar'=> // User Photo URL of WordPress User
7
'displayname'=> // User Display Name of WordPress User
8
'roles'=> // WordPress user role, Accepts Array of roles []
9
'caps'=> // WordPress user capability, Accepts Array of capabilities, edit_posts (instructors),manage_options(administrators), read (students)
10
'profile_link'=> //the Profile link
11
}
Copied!
There is further possibility to process the token using the filter and modify the user object stored in the token. For example, course data can be added to the token. However as JWT tokens are sometimes sent in request header, we recommend keeping the size to a limited less than 8kb.

Expanding the Token

In almost all addon / plugins that you will create you will be required to expand the JWT token sent in the API request. You can expand the JWT token can capture the User making the request. There are 2 ways to expand the token :
  1. 1.
    Use the VibeBP Filter on the Token: $user = apply_filters('vibebp_api_get_user_from_token','',$body['token']);
  2. 2.
    Use the vibebp_expand_token($token) function
Both above ways are correct. See following code snippet from our Vibe Zoom addon for expanding the token :
1
register_rest_route( VIBE_ZOOM_API_NAMESPACE, '/user/meetings/recordings', array(
2
'methods' => 'POST',
3
'callback' => array( $this, 'get_meeting_recording' ),
4
'permission_callback' => array( $this, 'user_permissions_check' ),
5
) );
6
7
8
function user_permissions_check($request){
9
//Validate token send in Post request
10
$body = json_decode($request->get_body(),true);
11
if(!empty($body['token'])){
12
global $wpdb;
13
$this->user = apply_filters('vibebp_api_get_user_from_token','',$body['token']);
14
if(!empty($this->user)){
15
return true;
16
}
17
}
18
return false;
19
}
20
21
function get_meetings($request){
22
global $wpdb,$bp;
23
24
$args = json_decode($request->get_body(),true);
25
$return = array(
26
'status'=>0,
27
'meetings'=>[]
28
);
29
30
$meeting_args = array(
31
'post_type'=>'vibe_zoom',
32
'posts_per_page'=>20,
33
'paged'=>empty($args['page'])?'':$args['page'],
34
's'=>empty($args['s'])?'':$args['s'],
35
'orderby'=>empty($args['orderby'])?'none':$args['orderby'],
36
'order'=>empty($args['order'])?'':$args['order'],
37
);
38
39
//Capture the USer ID from the token.
40
$meeting_args['author']=$this->user->id;
41
42
//Process using the user_id
Copied!
The token also have an expiry value that the administrator sets in the WP admin - VibeBP - Settings - General.
This token is also used in Amazon S3 uploads in S3 plugin, Vimeo Uploads in VideoVibe Plugin.
Last modified 1yr ago