|
1 | 1 | # Authentication |
2 | 2 |
|
3 | | -Because we have two parts (scoketio and rest) which must communicate |
4 | | -with the same server (which required maybe authentication) we must found a way to authenticate |
| 3 | +Because we have two parts (scketio and rest) which must communicate |
| 4 | +with the same server (which required maybe authentication) we must found a way to authenticate |
5 | 5 | both rest & socketio once user is logged. |
6 | 6 |
|
7 | | -Below, how to authenticate rest only or socketio only. But notice that in a production app, |
| 7 | +Below, how to authenticate rest only or socketio only. But notice that in a production app, |
8 | 8 | you don't need to auth speratly the different client, just use the global authentication method. |
9 | | -Nice ? Go |
| 9 | +Nice ? Go :rocket: |
10 | 10 |
|
11 | | -## Auth rest |
| 11 | +## Rest only |
12 | 12 |
|
13 | 13 | You can for testing purpose only authenticate the rest client by doing the following. |
14 | 14 | Note that when you use an other stratagy different from email/pass strategy, you must configure it on your server. |
15 | 15 |
|
16 | 16 | ```dart |
17 | | - // Auth with rest client with email/password [Default strategy] |
18 | | - var authResponse = await flutterFeathersjs.rest |
19 | | - .authenticate(userName: "mail@mail.com", password: "Strong_Pass"); |
20 | | -
|
21 | | - // Or |
22 | | -
|
23 | | - // Auth with rest client with phone/password strategy |
24 | | - var authResponse = await flutterFeathersjs.rest.authenticate( |
25 | | - strategy: "phone", |
26 | | - userNameFieldName: "tel", |
27 | | - userName:"+22900000000",, |
28 | | - password: "Strong_Pass"); |
| 17 | + try { |
| 18 | + var user = await flutterFeathersjs.rest.authenticate( |
| 19 | + userName: "dah.kenangnon@flutter_feathersjs.com", |
| 20 | + password: "flutter_feathersjs"); |
| 21 | + //TODO: Authentication is Ok, save user in local storage |
| 22 | +
|
| 23 | + } on FeatherJsError catch (e) { |
| 24 | + if (e.type == FeatherJsErrorType.IS_INVALID_CREDENTIALS_ERROR) { |
| 25 | + //TODO: Invalid credentials |
| 26 | + } else if (e.type == FeatherJsErrorType.IS_INVALID_STRATEGY_ERROR) { |
| 27 | + //TODO: Invalid strategy |
| 28 | + } else if (e.type == FeatherJsErrorType.IS_AUTH_FAILED_ERROR) { |
| 29 | + //TODO: Invalid authentication failed for other reason. |
| 30 | + // verbose => print(e.message); |
| 31 | + } |
| 32 | + //TODO: Check for other FeatherJsErrorType |
| 33 | + // => print(e.type); |
| 34 | + } catch (e) { |
| 35 | + //TODO: Authentication failed for unkknown reason. |
| 36 | + // why => print(e.type); |
| 37 | + // why => print(e.message); |
| 38 | + } |
29 | 39 |
|
30 | 40 | ``` |
31 | 41 |
|
32 | | -## Auth socketio |
| 42 | +## Socketio only |
33 | 43 |
|
34 | | -The process to authenticate the socketio client is done after rest auth is os because, |
35 | | -it using the JWT retrieved by rest client when process finished with ok. |
| 44 | +The process to authenticate the socketio client is done after rest auth is done because, |
| 45 | +it use the JWT retrieved by rest client when process finished with ok. |
36 | 46 |
|
37 | 47 | ```dart |
| 48 | +
|
38 | 49 | // Note: This must be call after rest auth success |
39 | 50 | // Not recommanded to use this directly |
40 | | - var authResponse = await flutterFeathersjs.scketio.authWithJWT(); |
| 51 | + try { |
| 52 | + bool isReAuthenticated = await flutterFeathersjs.scketio.authWithJWT(); |
| 53 | +
|
| 54 | + //print(isReAuthenticated); => true |
| 55 | +
|
| 56 | + } on FeatherJsError catch (e) { |
| 57 | + if (e.type == FeatherJsErrorType.IS_JWT_TOKEN_ERROR) { |
| 58 | + //TODO: Error using the JWT to authenticated |
| 59 | + // Redirect user to login page |
| 60 | + }else{ |
| 61 | + //TODO: Check for other FeatherJsErrorType |
| 62 | + // why => print(e.type); |
| 63 | + // why => print(e.message); |
| 64 | + } |
| 65 | + } catch (e) { |
| 66 | + //TODO: Authentication failed for unkknown reason. |
| 67 | + // why => print(e.type); |
| 68 | + // why => print(e.message); |
| 69 | + } |
41 | 70 |
|
42 | 71 | ``` |
43 | 72 |
|
| 73 | +## Global (recommended) |
44 | 74 |
|
| 75 | +### Autenticate |
45 | 76 |
|
46 | | -## Global Auth (recommanded) |
47 | | - |
48 | | -Definitely, this is what you must do when you want to authenticate your user. |
| 77 | +Go to login page, retrieve user credentials and authenticat user |
| 78 | +with different strategy |
49 | 79 |
|
50 | 80 | ```dart |
| 81 | + try { |
51 | 82 |
|
52 | | -/// ------ First time |
| 83 | + // Default strategy (email/password => local strategy) |
| 84 | + var user = await flutterFeathersjs.authenticate( |
| 85 | + userName: "dah.kenangnon@flutter_feathersjs.com", |
| 86 | + password: "flutter_feathersjs"); |
53 | 87 |
|
54 | | -// Auth globaly with phone number strategy |
55 | | -// When using this strategy: ensure you configure your feathers js server accordingly |
56 | | - var rep = await flutterFeathersjs.authenticate( |
| 88 | + // Or use what you want, e.g: phone/password |
| 89 | + // Auth with rest client with phone/password strategy |
| 90 | + // Note: You must configure your server for this strategy to work |
| 91 | + var user = await flutterFeathersjs.authenticate( |
57 | 92 | strategy: "phone", |
58 | | - userNameFieldName: "tel", |
59 | | - userName: "+22900000000", |
60 | | - password: "Strong_Pass"); |
61 | | -
|
62 | | -
|
63 | | -
|
64 | | -
|
65 | | -// Auth globaly with email strategy [default] |
66 | | - var rep = await flutterFeathersjs.authenticate( |
67 | | - userName: "mail@mail.com", |
68 | | - password: "Strong_Pass"); |
69 | | -
|
70 | | -/// ------ On app restart or when JWT still validated |
71 | | - var reps = await flutterFeathersjs.reAuthenticate(); |
72 | | -
|
73 | | - if (!reps["error"]) { |
74 | | - print('client is authed'); |
75 | | - print("----------Authed user :------"); |
76 | | - print(reps["message"]); // User is under reps["message"] when all thing is Ok |
77 | | - print("----------Authed user :------"); |
78 | | - } else |
79 | | - { |
80 | | - print(reps["message"]); // Error message is under reps["message"] when something is wrong |
| 93 | + userNameFieldName: "tel", // "tel" is the fieldname on the mongoose|? model |
| 94 | + userName:"+22900000000", |
| 95 | + password: "flutter_feathersjs"); |
| 96 | +
|
| 97 | +
|
| 98 | + //TODO: Authentication is Ok, save user in local storage |
| 99 | +
|
| 100 | + } on FeatherJsError catch (e) { |
| 101 | + if (e.type == FeatherJsErrorType.IS_INVALID_CREDENTIALS_ERROR) { |
| 102 | + //TODO: Invalid credentials |
| 103 | + } else if (e.type == FeatherJsErrorType.IS_INVALID_STRATEGY_ERROR) { |
| 104 | + //TODO: Invalid strategy |
| 105 | + } else if (e.type == FeatherJsErrorType.IS_AUTH_FAILED_ERROR) { |
| 106 | + //TODO: Invalid authentication failed for other reason. |
| 107 | + // verbose => print(e.message); |
| 108 | + } |
| 109 | + //TODO: Check for other FeatherJsErrorType |
| 110 | + // => print(e.type); |
| 111 | + } catch (e) { |
| 112 | + //TODO: Authentication failed for unkknown reason. |
| 113 | + // why => print(e.type); |
| 114 | + // why => print(e.message); |
| 115 | + } |
| 116 | +``` |
81 | 117 |
|
82 | | - // If you want to check when error is comming from socketio client |
83 | | - // Error message from socketio client, |
84 | | - print(reps["scketResponse"]); |
| 118 | +## ReAuthenticate on app restarted |
85 | 119 |
|
| 120 | +Then reAutenticate user, if JWT still validated without request credentials from user on app restart |
86 | 121 |
|
87 | | - // If you want to check when error is comming from rest client |
88 | | - // Error message from rest client, |
89 | | - print(reps["restResponse"]); |
| 122 | +```dart |
| 123 | + try { |
| 124 | + bool isReAuthenticated = await flutterFeathersjs.reAuthenticate(); |
| 125 | +
|
| 126 | + //print(isReAuthenticated); => true |
| 127 | +
|
| 128 | + } on FeatherJsError catch (e) { |
| 129 | + if (e.type == FeatherJsErrorType.IS_AUTH_FAILED_ERROR) { |
| 130 | + //TODO: ReAutentication failed |
| 131 | + // Redirect user to login page |
| 132 | + }else{ |
| 133 | + //TODO: Check for other FeatherJsErrorType |
| 134 | + // why => print(e.type); |
| 135 | + // why => print(e.message); |
| 136 | + } |
| 137 | + } catch (e) { |
| 138 | + //TODO: Authentication failed for unkknown reason. |
| 139 | + // why => print(e.type); |
| 140 | + // why => print(e.message); |
90 | 141 | } |
91 | | -``` |
| 142 | +``` |
0 commit comments