@stanlemon/react-couchdb-authentication
Advanced tools
Comparing version 0.2.7 to 0.2.8
@@ -102,2 +102,3 @@ import * as React from "react"; | ||
private fetch; | ||
private grabSession; | ||
private checkSession; | ||
@@ -104,0 +105,0 @@ private logout; |
@@ -156,18 +156,30 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
grabSession() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// If we have a remoteDb, we'll use it. This works better in Safari which does | ||
// not support storing cross-origin cookies across multiple requests. | ||
if (this.remoteDb) { | ||
// This is not exposed via the TypeScript definition for PouchDB.Database | ||
// but it is added by the HTTP adapter, and we've accounted for it on the property | ||
const user = yield this.remoteDb | ||
.fetch(`../_users/org.couchdb.user:${this.state.user.name}`) | ||
.then((d) => d.json()); | ||
return user; | ||
} | ||
else { | ||
const session = yield this.fetch(this.props.url + "_session"); | ||
return session.userCtx; | ||
} | ||
}); | ||
} | ||
checkSession() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
// If we have a remoteDb, we'll use it. This works better in Safari which does | ||
// not support storing cross-origin cookies across multiple requests. | ||
const session = this.remoteDb | ||
? // This is not exposed via the TypeScript definition for PouchDB.Database | ||
// but it is added by the HTTP adapter, and we've accounted for it on the property | ||
yield this.remoteDb.fetch("../_session").then((d) => d.json()) | ||
: yield this.fetch(this.props.url + "_session"); | ||
this.log("User session", session); | ||
const isLoggedIn = !!session.userCtx.name; | ||
const user = yield this.grabSession(); | ||
this.log("User session", user); | ||
const isLoggedIn = user !== null && user.name !== null; | ||
this.setState({ | ||
loaded: true, | ||
authenticated: isLoggedIn, | ||
user: session.userCtx, | ||
user, | ||
}); | ||
@@ -178,2 +190,4 @@ // If we are logged in and have not yet setup our remote db connection, set it up | ||
this.setupDb(); | ||
// Immediately check the session again so we fully load the user | ||
this.checkSession(); | ||
} | ||
@@ -180,0 +194,0 @@ } |
{ | ||
"name": "@stanlemon/react-couchdb-authentication", | ||
"version": "0.2.7", | ||
"version": "0.2.8", | ||
"description": "React component for authenticating against a CouchDB user db and syncing it locally with PouchDB.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
51031
634