通过移动应用程序使用Moodle进行身份验证

编程入门 行业动态 更新时间:2024-10-27 14:32:04
本文介绍了通过移动应用程序使用Moodle进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的移动应用程序需要登录Moodle才能从Web服务获取Json数据并使用Angular进行显示.

My mobile app needs to log in to Moodle to get Json data from a webservice and display it using Angular.

为此,我需要输入用户名和密码并获取Moodle Web服务令牌,因此我的应用无需再次登录(至少在令牌过期之前).

In order to do that, I need to pass in a username and password and get a Moodle webservice token back, so my app doesn't need to log in again (at least until the token expires).

(这是询问并回答自己的问题"之一,因此下面是我的解决方案,但欢迎提出评论和建议.)

(this is one of those "ask and answer your own question" things, so my solution is below, but comments & suggestions welcome.)

多亏了我用来创建此解决方案的所有其他StackOverflow页面!

With thanks to all the other StackOverflow pages I have used to create this solution!

另请参见-如何获取Angular从Moodle Web服务获取数据.

推荐答案

步骤1.检查令牌是否已存在

jQuery(document).ready(function () { /* when the user clicks log-out button, destroy the session */ $('#btn_logout').on('click', function () { $('.pane').hide(); /* hide all screens */ $('#menu').toggleClass('ui-panel-open ui-panel-closed'); $.jStorage.deleteKey('session'); makeUserLogin(); }); var session = $.jStorage.get('session', ''); // syntax: $.jStorage.get(keyname, "default value") if (session) { // if there is already a session, redirect to landing pane showApp(); } else { // if there is no session *then* redirect to the login pane makeUserLogin(); } });

第2步.创建功能以显示应用&重定向到登录页面

function showApp() { $('#home-pane').show(); /* show home screen */ $('#system-message').hide(); $('#login-pane').hide(); /* hide login screen*/ $('#menu_btn').removeClass('hidden'); /* show menu button so user can see rest of app */ } function makeUserLogin() { $('#btn_login').click(function () { console.log('click event for login_button'); var username = $('#username').val(); var password = $('#password').val(); postCredentials(username, password, createSession); }); $('#menu_btn').addClass('hidden'); /* hide menu button so user cannot see rest of app */ $('#home-pane').hide(); /* hide home screen */ $('#login-pane').show(); /* show login screen */ } function postCredentials(username, password, callback) { if ((username.length && password.length) && (username !== '' && password !='')) { var url = 'moodle.yourcompany/local/login/token.php'; $.post(url, { username: username, password: password, service: 'webservice_ws' // your webservice name }).done(function (data) { token = data.token; dataString = JSON.stringify(data); if (dataString.indexOf('error') > 0) { showErrorDialog('<p class="error">Invalid user credentials, please try again</p>'); } else { createSession(token); } }).fail(function () { showErrorDialog('<p class="error">Login failed</p>'); }); } else { showErrorDialog('<p class="error">Please enter a username and password</p>'); } } function createSession(token) { // syntax: $.jStorage.set('keyname', 'keyvalue', {TTL: milliseconds}); // {TTL... is optional time, in milliseconds, until key/value pair expires} $.jStorage.set('session', token, { TTL: 28800000 }); // redirect to whatever page you need after a successful login showApp(); } function showErrorDialog(errorMsg) { $('#system-message').html(errorMsg); $('#system-message').fadeIn(); }

更多推荐

通过移动应用程序使用Moodle进行身份验证

本文发布于:2023-06-07 22:02:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/596871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   身份验证   Moodle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!