Skip to content

Commit

Permalink
Init lix.Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinresol committed Oct 10, 2018
1 parent eacac27 commit bc22495
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 28 deletions.
2 changes: 1 addition & 1 deletion build.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
-lib coconut.vdom
-lib coconut.router
-lib tink_web
-lib lix.server
-lib lix.sdk
-lib hxjs-amazon-cognito-auth-js
5 changes: 5 additions & 0 deletions haxe_libraries/archive.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-D archive=0.1.0
# @install: lix --silent download "haxelib:/archive#0.1.0" into archive/0.1.0/haxelib
-lib tink_io
-lib format
-cp ${HAXE_LIBCACHE}/archive/0.1.0/haxelib/src
7 changes: 7 additions & 0 deletions haxe_libraries/asys.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-D asys=0.3.0
# @install: lix --silent download "haxelib:/asys#0.3.0" into asys/0.3.0/haxelib
-lib tink_core
-lib tink_macro
-lib tink_io
-lib tink_streams
-cp ${HAXE_LIBCACHE}/asys/0.3.0/haxelib/
3 changes: 3 additions & 0 deletions haxe_libraries/format.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-D format=3.4.1
# @install: lix --silent download "haxelib:/format#3.4.1" into format/3.4.1/haxelib
-cp ${HAXE_LIBCACHE}/format/3.4.1/haxelib/
4 changes: 4 additions & 0 deletions haxe_libraries/futurize.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-D futurize=1.0.1
# @install: lix --silent download "haxelib:/futurize#1.0.1" into futurize/1.0.1/haxelib
-lib tink_macro
-cp ${HAXE_LIBCACHE}/futurize/1.0.1/haxelib/src
9 changes: 9 additions & 0 deletions haxe_libraries/lix.sdk.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-D lix.sdk=0.0.0
# @install: lix --silent download "gh://github.com/lix-pm/lix.sdk#c9cb0ba42c7682d1549045f7ffd0bd8d80aa550a" into lix.sdk/0.0.0/github/c9cb0ba42c7682d1549045f7ffd0bd8d80aa550a
-lib asys
-lib archive
-lib lix.server
-lib tink_await
-lib tink_web
# -cp ${HAXE_LIBCACHE}/lix.sdk/0.0.0/github/c9cb0ba42c7682d1549045f7ffd0bd8d80aa550a/src
-cp ../lix.sdk/src
5 changes: 3 additions & 2 deletions haxe_libraries/lix.server.hxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-D lix.server=0.0.0
# @install: lix --silent download "gh://github.com/lix-pm/lix.server#50f5f7b3ddca8c8211a4fdff936e3dab9b5c0de9" into lix.server/0.0.0/github/50f5f7b3ddca8c8211a4fdff936e3dab9b5c0de9
# @install: lix --silent download "gh://github.com/lix-pm/lix.server#8b40ec7e267f6f9a8064d0babd9a234f301a31a9" into lix.server/0.0.0/github/8b40ec7e267f6f9a8064d0babd9a234f301a31a9
-lib why-fs
-lib tink_semver
-lib tink_sql
-cp ${HAXE_LIBCACHE}/lix.server/0.0.0/github/50f5f7b3ddca8c8211a4fdff936e3dab9b5c0de9/lib
-cp ${HAXE_LIBCACHE}/lix.server/0.0.0/github/8b40ec7e267f6f9a8064d0babd9a234f301a31a9/lib
4 changes: 4 additions & 0 deletions haxe_libraries/mime.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-D mime=0.0.1
# @install: lix --silent download "haxelib:/mime#0.0.1" into mime/0.0.1/haxelib
-cp ${HAXE_LIBCACHE}/mime/0.0.1/haxelib/
--macro mime.Mime.init()
7 changes: 7 additions & 0 deletions haxe_libraries/tink_await.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-D tink_await=0.4.0
# @install: lix --silent download "haxelib:/tink_await#0.4.0" into tink_await/0.4.0/haxelib
-lib tink_syntaxhub
-lib tink_macro
-lib tink_core
-cp ${HAXE_LIBCACHE}/tink_await/0.4.0/haxelib/src
--macro tink.await.Await.use()
5 changes: 3 additions & 2 deletions haxe_libraries/tink_http.hxml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-D tink_http=0.8.1
# @install: lix --silent download "gh://github.com/haxetink/tink_http#ed67a8436c17f972f3036effbcc6dceb54826fa5" into tink_http/0.8.1/github/ed67a8436c17f972f3036effbcc6dceb54826fa5
# @install: lix --silent download "gh://github.com/haxetink/tink_http#ed61715675e4305bf6fff7c7422a2cb3569cc0e2" into tink_http/0.8.1/github/ed61715675e4305bf6fff7c7422a2cb3569cc0e2
-lib tink_url
-lib http-status
-lib tink_io
-lib tink_state
-lib tink_anon
-cp ${HAXE_LIBCACHE}/tink_http/0.8.1/github/ed67a8436c17f972f3036effbcc6dceb54826fa5/src
-cp ${HAXE_LIBCACHE}/tink_http/0.8.1/github/ed61715675e4305bf6fff7c7422a2cb3569cc0e2/src
4 changes: 2 additions & 2 deletions haxe_libraries/tink_semver.hxml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-D tink_semver=0.0.0
# @install: lix --silent download "gh://github.com/haxetink/tink_semver#5027a6c62cb16ca4644abed69b3fe5839d613754" into tink_semver/0.0.0/github/5027a6c62cb16ca4644abed69b3fe5839d613754
# @install: lix --silent download "gh://github.com/haxetink/tink_semver#c7de0c2e6e5551f3beeb8fbd4e6a5bbe44a5f625" into tink_semver/0.0.0/github/c7de0c2e6e5551f3beeb8fbd4e6a5bbe44a5f625
-lib tink_core
-lib tink_parse
-cp ${HAXE_LIBCACHE}/tink_semver/0.0.0/github/5027a6c62cb16ca4644abed69b3fe5839d613754/src
-cp ${HAXE_LIBCACHE}/tink_semver/0.0.0/github/c7de0c2e6e5551f3beeb8fbd4e6a5bbe44a5f625/src
6 changes: 3 additions & 3 deletions haxe_libraries/tink_web.hxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-D tink_web=0.1.2
# @install: lix --silent download "gh://github.com/haxetink/tink_web#d8d8b47c50bc286770df0c517b586c3c41a3ed7c" into tink_web/0.1.2/github/d8d8b47c50bc286770df0c517b586c3c41a3ed7c
-D tink_web=0.1.3
# @install: lix --silent download "gh://github.com/haxetink/tink_web#43c95a00fcacbfcd38ece60741dfbfdad78fff23" into tink_web/0.1.3/github/43c95a00fcacbfcd38ece60741dfbfdad78fff23
-lib tink_http
-lib tink_json
-lib tink_querystring
-cp ${HAXE_LIBCACHE}/tink_web/0.1.2/github/d8d8b47c50bc286770df0c517b586c3c41a3ed7c/src
-cp ${HAXE_LIBCACHE}/tink_web/0.1.3/github/43c95a00fcacbfcd38ece60741dfbfdad78fff23/src
7 changes: 7 additions & 0 deletions haxe_libraries/why-fs.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-D why-fs=0.0.0
# @install: lix --silent download "gh://github.com/kevinresol/why-fs#b511b71e0b2b93088e814ac9ca320cbf32dbe1a0" into why-fs/0.0.0/github/b511b71e0b2b93088e814ac9ca320cbf32dbe1a0
-lib tink_http
-lib tink_io
-lib futurize
-lib mime
-cp ${HAXE_LIBCACHE}/why-fs/0.0.0/github/b511b71e0b2b93088e814ac9ca320cbf32dbe1a0/src
3 changes: 2 additions & 1 deletion src/lix/site/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Main extends coconut.ui.View {
trace(Macro.getBuildDate());
trace(Macro.getGitSha());


trace(new CognitoAuth().getSignInUserSession().idToken.payload);
trace(new CognitoAuth().getSignInUserSession().accessToken.payload);

var data = new AppData({
user: new UserData({
Expand Down
46 changes: 42 additions & 4 deletions src/lix/site/data/CognitoAuth.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,57 @@ package lix.site.data;
import lix.util.Config.*;
import js.Browser.*;

@:forward
abstract CognitoAuth(aws.cognito.CognitoAuth) {
using tink.CoreApi;

class CognitoAuth {
public var result:Promise<Session>;
public var isSignedIn(get, never):Bool;
var impl:aws.cognito.CognitoAuth;

public function new() {
this = new aws.cognito.CognitoAuth({
impl = new aws.cognito.CognitoAuth({
ClientId: 'fvrf50i7h5od9nr1bq4pefcg3',
AppWebDomain: 'login.lix.pm',
RedirectUriSignIn: '$SITE_URL/login',
RedirectUriSignOut: SITE_URL,
TokenScopesArray: ['openid'],
UserPoolId: 'us-east-2_qNnxj1mU1',
});

result = Future.async(function(cb) {
impl.userhandler = {
onSuccess: session -> cb(Success({
idToken: session.idToken.jwtToken,
accessToken: session.accessToken.jwtToken,
refreshToken: session.refreshToken.refreshToken,
scopes: session.tokenScopes.tokenScopes,
})),
onFailure: e -> cb(Failure(Error.ofJsError(e))),
}
});
}

public inline function getSignInUserSession() {
return impl.getSignInUserSession();
}

public inline function getSession() {
impl.getSession();
}

public inline function signOut() {
impl.signOut();
}

public inline function parseUrl()
return this.parseCognitoWebResponse(window.location.href);
return impl.parseCognitoWebResponse(window.location.href);

inline function get_isSignedIn() return impl.isUserSignedIn();
}

typedef Session = {
final idToken:String;
final accessToken:String;
final refreshToken:String;
final scopes:List<String>;
}
14 changes: 14 additions & 0 deletions src/lix/site/data/UserData.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package lix.site.data;

import tink.http.clients.*;

using tink.CoreApi;

class UserData implements coconut.data.Model {
@:editable var id:String = @byDefault null;
@:loaded var data:lix.api.types.User =
new lix.Remote(new JsClient(), () -> {
#if (environment == "local")
'2';
#else
var auth = new CognitoAuth();
auth.getSession();
auth.result.next(session -> session.idToken);
#end
}).me().get();
}
5 changes: 3 additions & 2 deletions src/lix/site/ui/HomePage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class HomePage extends coconut.ui.View {
<case ${null}>
<button onclick=${login}>Login</button>
<case ${id}>
<span>Logged in as ${id}</span>
<button onclick=${logout}>Logout</button>
<div>Logged in as ${id}</div>
<pre>${haxe.Json.stringify(app.user.data, ' ')}</pre>
<div><button onclick=${logout}>Logout</button></div>
</switch>
</div>
';
Expand Down
17 changes: 6 additions & 11 deletions src/lix/site/ui/LoginPage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ class LoginPage extends coconut.ui.View {

override function afterInit(e) {
var auth = new CognitoAuth();

auth.userhandler = {
onSuccess: function(session) {
app.user.id = switch try session.idToken.payload.sub catch(e:Dynamic) null {
case null: try Json.parse(Base64.decode(session.idToken.jwtToken.split('.')[1]).toString()).sub catch(e:Dynamic) null;
case v: v;
}
auth.result.handle(function(o) switch o {
case Success(session):
app.user.id = try Json.parse(Base64.decode(session.idToken.split('.')[1]).toString()).sub catch(e:Dynamic) null;
// TODO: store redirect url in local storage before initializing the login sequence
spectatory.Location.replace('/');
},
onFailure: function(result) trace(result),
}

case Failure(e):
trace(e);
});
auth.parseUrl();
}
}

0 comments on commit bc22495

Please sign in to comment.