基本的登录框(Basic Login)

简介: This is my first stab at a tutorial, so hopefully it works out! Thanks to crafter for his code examples in this thread: http://extjs.
This is my first stab at a tutorial, so hopefully it works out! Thanks to crafter for his code examples in this thread: http://extjs.com/forum/showthread.php?t=26320


Contents

[hide]

img_d05563fa34a9855c9c94ef41225fc3ec.png Entry Point

Let's assume the entry point to your application is index.asp, and it is structured as follows:


<html>
	<head>
	<link>	
	<script></script>
	<script></script>
	<script></script>	
	</head>
	<body></body>
</html>

Obviously, modify the paths to your EXT directory accordingly. See the source for Login.js below

img_d05563fa34a9855c9c94ef41225fc3ec.png Login.js

Next comes login.js. This guy handles all the heavy lifting, and for me, has all the pieces I was missing coming from a more traditional way of thinking about user authentication. It creates the form, renders it to a popup window, presents the window to the user, sends the submission via ajax, and handles the success and failure response depending on whether your user entered successful credentials.

img_405b18b4b6584ae338e0f6ecaf736533.gif img_1c53668bcee393edac0d7b3b3daff1ae.gif Ext.onReady( function () img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif {
img_33d02437d135341f0800e3d415312ae8.gif    Ext.QuickTips.init();
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif    
// Create a variable to hold our EXT Form Panel. 
img_33d02437d135341f0800e3d415312ae8.gif
    // Assign various config options as seen.     
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
    var login = new Ext.FormPanel(img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif        labelWidth:
80,
img_33d02437d135341f0800e3d415312ae8.gif        url:
'login.asp'
img_33d02437d135341f0800e3d415312ae8.gif        frame:
true
img_33d02437d135341f0800e3d415312ae8.gif        title:
'Please Login'
img_33d02437d135341f0800e3d415312ae8.gif        width:
230
img_33d02437d135341f0800e3d415312ae8.gif        padding:
200
img_33d02437d135341f0800e3d415312ae8.gif        defaultType:
'textfield',
img_33d02437d135341f0800e3d415312ae8.gif    monitorValid:
true,
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif    
// Specific attributes for the text fields for username / password. 
img_33d02437d135341f0800e3d415312ae8.gif
    // The "name" attribute defines the name of variables sent to the server.
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
        items:[img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif                fieldLabel:
'Username'
img_33d02437d135341f0800e3d415312ae8.gif                name:
'loginUsername'
img_33d02437d135341f0800e3d415312ae8.gif                allowBlank:
false 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif            }
,img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif                fieldLabel:
'Password'
img_33d02437d135341f0800e3d415312ae8.gif                name:
'loginPassword'
img_33d02437d135341f0800e3d415312ae8.gif                inputType:
'password'
img_33d02437d135341f0800e3d415312ae8.gif                allowBlank:
false 
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }
],
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif    
// All the magic happens after the user clicks the button     
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
        buttons:[img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif                text:
'Login',
img_33d02437d135341f0800e3d415312ae8.gif                formBind: 
true,     
img_33d02437d135341f0800e3d415312ae8.gif                
// Function that fires when user clicks the button 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
                handler:function()img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                    login.getForm().submit(
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif                        method:
'POST'
img_33d02437d135341f0800e3d415312ae8.gif                        waitTitle:
'Connecting'
img_33d02437d135341f0800e3d415312ae8.gif                        waitMsg:
'Sending dataimg_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif',
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif            
// Functions that fire (success or failure) when the server responds. 
img_33d02437d135341f0800e3d415312ae8.gif
            // The one that executes is determined by the 
img_33d02437d135341f0800e3d415312ae8.gif
            // response that comes from login.asp as seen below. The server would 
img_33d02437d135341f0800e3d415312ae8.gif
            // actually respond with valid JSON, 
img_33d02437d135341f0800e3d415312ae8.gif
            // something like: response.write "{ success: true}" or 
img_33d02437d135341f0800e3d415312ae8.gif
            // response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
img_33d02437d135341f0800e3d415312ae8.gif
            // depending on the logic contained within your server script.
img_33d02437d135341f0800e3d415312ae8.gif
            // If a success occurs, the user is notified with an alert messagebox, 
img_33d02437d135341f0800e3d415312ae8.gif
            // and when they click "OK", they are redirected to whatever page
img_33d02437d135341f0800e3d415312ae8.gif
            // you define as redirect. 
img_33d02437d135341f0800e3d415312ae8.gif
 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                        success:
function()img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                            Ext.Msg.alert(
'Status''Login Successful!'function(btn, text)img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                   
if (btn == 'ok')img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif                                
var redirect = 'test.asp'
img_33d02437d135341f0800e3d415312ae8.gif                                window.location 
= redirect;
img_105a1e124122b2abcee4ea8e9f5108f3.gif                                   }

img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }
);
img_105a1e124122b2abcee4ea8e9f5108f3.gif                        }
,
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif            
// Failure function, see comment above re: success and failure. 
img_33d02437d135341f0800e3d415312ae8.gif
            // You can see here, if login fails, it throws a messagebox
img_33d02437d135341f0800e3d415312ae8.gif
            // at the user telling him / her as much.  
img_33d02437d135341f0800e3d415312ae8.gif
 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                        failure:
function(form, action)img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                            
if(action.failureType == 'server')img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif                                obj 
= Ext.util.JSON.decode(action.response.responseText); 
img_33d02437d135341f0800e3d415312ae8.gif                                Ext.Msg.alert(
'Login Failed!', obj.errors.reason); 
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif                            }
elseimg_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif
img_33d02437d135341f0800e3d415312ae8.gif                                Ext.Msg.alert(
'Warning!''Authentication server is unreachable : ' + action.response.responseText); 
img_105a1e124122b2abcee4ea8e9f5108f3.gif                            }
 
img_33d02437d135341f0800e3d415312ae8.gif                            login.getForm().reset(); 
img_105a1e124122b2abcee4ea8e9f5108f3.gif                        }
 
img_105a1e124122b2abcee4ea8e9f5108f3.gif                    }
); 
img_105a1e124122b2abcee4ea8e9f5108f3.gif                }
 
img_105a1e124122b2abcee4ea8e9f5108f3.gif            }

img_105a1e124122b2abcee4ea8e9f5108f3.gif    }
);
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif 
img_33d02437d135341f0800e3d415312ae8.gif    
// This just creates a window to wrap the login form. 
img_33d02437d135341f0800e3d415312ae8.gif
    // The login object is passed to the items collection.       
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif
    var win = new Ext.Window(img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif        layout:
'fit',
img_33d02437d135341f0800e3d415312ae8.gif        width:
300,
img_33d02437d135341f0800e3d415312ae8.gif        height:
150,
img_33d02437d135341f0800e3d415312ae8.gif        closable: 
false,
img_33d02437d135341f0800e3d415312ae8.gif        resizable: 
false,
img_33d02437d135341f0800e3d415312ae8.gif        plain: 
true,
img_33d02437d135341f0800e3d415312ae8.gif        items: [login]
img_105a1e124122b2abcee4ea8e9f5108f3.gif    }
);
img_33d02437d135341f0800e3d415312ae8.gif    win.show();
img_05dd8d549cff04457a6366b0a7c9352a.gif}
);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

img_d05563fa34a9855c9c94ef41225fc3ec.png Login.asp

Here is the server processing for your login. I'm going to paste the following overly simplistic code to show the responses that go back, and ultimately determine which function in login.js fires (success or failure). However, this is where you would make the call to the database with the username/password variables, do your authentication, and then send either of these responses depending on whether or not what the user provided a valid set of credentials.


 
 . =  
	. 

	. 
 
 

img_d05563fa34a9855c9c94ef41225fc3ec.png Login.php

<?php
 =  ?  : ;
 
 == 
     ;
  
     ;

?>

img_d05563fa34a9855c9c94ef41225fc3ec.png Login.cfm



    

    

img_d05563fa34a9855c9c94ef41225fc3ec.png Test.asp

You will notice a line in login.js that redirects to Test.asp if a successful login happens. This can obviously be whatever page your main application will be. In my situation, users can have any number of combinations of navigation options, so the next step would be to validate with whatever session management mecahanism you use, that the person accessing test.asp is authenticated to do so. Further, I would pull down whatever navigation options are assigned to that user and build my toolbar accordingly. Since I'm still trying to figure that part out, that will be another tutorial :)

Hopefully this is somewhat helpful and thanks again to crafter for most of the js code.

目录
相关文章
|
3天前
|
缓存 移动开发 前端开发
登录报下面错误The remembered identity will be forgotten and not used for this request
登录报下面错误The remembered identity will be forgotten and not used for this request
|
3天前
|
数据库 数据安全/隐私保护
Access设置或取消密码
Access设置或取消密码
|
6月前
location search 属性获取登录用户名
location search 属性获取登录用户名
22 1
|
6月前
Please enter your authorization code to login. More information in http://service.mail.qq.com/
Please enter your authorization code to login. More information in http://service.mail.qq.com/
50 0
|
PHP
【laravel】在使用Auth认证时,登录后直接进入home,不登录会直接跳转到login
【laravel】在使用Auth认证时,登录后直接进入home,不登录会直接跳转到login
254 0
【laravel】在使用Auth认证时,登录后直接进入home,不登录会直接跳转到login
|
数据安全/隐私保护
SAP Spartacus的登录验证机制 - user login Authentication
SAP Spartacus的登录验证机制 - user login Authentication
107 0
SAP Spartacus的登录验证机制 - user login Authentication
SAP Spartacus login 超链接和 login form 的区别
SAP Spartacus login 超链接和 login form 的区别
SAP Spartacus login 超链接和 login form 的区别
Cloud for Customer mobile应用里pass Code登录界面的绘制原理
Cloud for Customer mobile应用里pass Code登录界面的绘制原理
123 0
Cloud for Customer mobile应用里pass Code登录界面的绘制原理
如何将SAP Spartacus的默认home页面替换成login页面 - ProtectedRoutesService
如何将SAP Spartacus的默认home页面替换成login页面 - ProtectedRoutesService
如何将SAP Spartacus的默认home页面替换成login页面 - ProtectedRoutesService