Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added console logging for token errors. #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Conduit/AppM.purs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Conduit.Capability.Routing (class MonadRouting)
import Conduit.Capability.Routing as Routing
import Conduit.Component.Auth (AuthIO)
import Conduit.Component.Routing (RoutingIO)
import Conduit.Config as Config
import Conduit.Data.Article (Article, ArticleRep, articleCodec, defaultArticlesQuery, mkArticleRepCodec)
import Conduit.Data.Auth (toAuth)
import Conduit.Data.Comment (Comment, commentCodec)
Expand All @@ -29,13 +30,14 @@ import Control.Monad.Reader (ReaderT, ask, asks, runReaderT)
import Data.Codec.Argonaut (JsonCodec)
import Data.Codec.Argonaut as CA
import Data.Codec.Argonaut.Record as CAR
import Data.Either (Either)
import Data.Either (Either, isLeft, fromLeft, hush)
import Data.Foldable (for_)
import Data.HTTP.Method (Method(..))
import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Effect.Aff.Class (class MonadAff)
import Effect.Class (class MonadEffect, liftEffect)
import Effect.Class.Console as Console
import Effect.Exception as Exception
import React.Halo as Halo
import Record as Record
Expand Down Expand Up @@ -102,12 +104,18 @@ instance UserRepository AppM where
loginUser credentials = do
(res :: Either Error { user :: CurrentUser }) <- makeRequest POST (StatusCode 200) Endpoint.Login loginBodyCodec userResponseCodec { user: credentials }
for_ res \{ user: currentUser } -> do
Auth.modify $ const $ toAuth currentUser.token (Just $ Record.delete (Proxy :: _ "token") currentUser)
let auth = toAuth currentUser.token (Just $ Record.delete (Proxy :: _ "token") currentUser)
when (isLeft auth && Config.nodeEnv /= "production") do
Console.log $ fromLeft "" auth
Auth.modify $ const $ hush auth
pure $ res <#> _.user
registerUser user = do
(res :: Either Error { user :: CurrentUser }) <- makeRequest POST (StatusCode 200) Endpoint.Users registerBodyCodec userResponseCodec { user }
for_ res \{ user: currentUser } -> do
Auth.modify $ const $ toAuth currentUser.token (Just $ Record.delete (Proxy :: _ "token") currentUser)
let auth = toAuth currentUser.token (Just $ Record.delete (Proxy :: _ "token") currentUser)
when (isLeft auth && Config.nodeEnv /= "production") do
Console.log $ fromLeft "" auth
Auth.modify $ const $ hush auth
pure $ res <#> _.user
updateUser user = do
(res :: Either Error { user :: CurrentUser }) <- makeSecureRequest PUT (StatusCode 200) Endpoint.User updateUserBodyCodec userResponseCodec { user }
Expand Down
14 changes: 11 additions & 3 deletions src/Conduit/Component/Auth.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import Prelude
import Affjax.StatusCode (StatusCode(..))
import Conduit.Api.Client (Error, makeSecureRequest')
import Conduit.Api.Endpoint as Endpoint
import Conduit.Config as Config
import Conduit.Data.Auth (Auth, toAuth)
import Conduit.Data.User (CurrentUser, currentUserCodec)
import Data.Codec.Argonaut as CA
import Data.Codec.Argonaut.Record as CAR
import Data.Either (Either, hush)
import Data.Either (Either, isLeft, fromLeft, hush)
import Data.Foldable (for_, traverse_)
import Data.HTTP.Method (Method(..))
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested (type (/\), (/\))
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Effect.Class.Console as Console
import Effect.Ref as Ref
import Effect.Timer as Timer
import Foreign.Day (now)
Expand Down Expand Up @@ -71,7 +73,9 @@ mkAuthManager = do
load = do
localStorage <- Window.localStorage =<< window
item <- Storage.getItem "token" localStorage
pure $ flip toAuth Nothing =<< item
pure $ flip toAuth' Nothing =<< item
where
toAuth' token user = hush $ toAuth token user

save = case _ of
Nothing -> do
Expand All @@ -92,4 +96,8 @@ mkAuthManager = do
(res :: Either Error { user :: CurrentUser }) <- makeSecureRequest' token GET (StatusCode 200) Endpoint.User CA.null (CAR.object "UserResponse" { user: currentUserCodec }) unit
liftEffect case hush $ _.user <$> res of
Nothing -> void $ modify $ const Nothing
Just user -> void $ modify $ const $ toAuth user.token (Just $ Record.delete (Proxy :: Proxy "token") user)
Just user -> do
let auth' = toAuth user.token (Just $ Record.delete (Proxy :: _ "token") user)
when (isLeft auth' && Config.nodeEnv /= "production") do
Console.log $ fromLeft "" auth'
void $ modify $ const $ hush auth'
11 changes: 7 additions & 4 deletions src/Conduit/Data/Auth.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude
import Conduit.Data.Jwt as Jwt
import Conduit.Data.User (User)
import Conduit.Data.Username (Username)
import Data.Either (hush)
import Data.Either (Either(..))
import Data.Maybe (Maybe)
import Data.Time.Duration (Milliseconds(..))
import Foreign.Day (DateTime, fromMilliseconds)
Expand All @@ -17,7 +17,10 @@ type Auth =
}

-- | Helpers
toAuth :: String -> Maybe User -> Maybe Auth
toAuth :: String -> Maybe User -> Either String Auth
toAuth token user = do
{ exp, username } <- hush $ Jwt.decode token
pure { token, username, expirationTime: fromMilliseconds $ Milliseconds $ exp * 1000.0, user }
let jwt = Jwt.decode token
case jwt of
Left e -> Left $ show e
Right { exp, username } ->
pure { token, username, expirationTime: fromMilliseconds $ Milliseconds $ exp * 1000.0, user }
6 changes: 6 additions & 0 deletions src/Conduit/Data/Jwt.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ data Error
| JSONParseError String
| JSONDecodeError CA.JsonDecodeError

instance showError :: Show Error where
show (MalformedToken) = "MalformedToken"
show (Base64DecodeError e) = "Base64DecodeError: " <> (show e)
show (JSONParseError e) = "JSONParseError: " <> e
show (JSONDecodeError e) = "JSONDecodeError: " <> (show e)

-- | Helpers
decode :: String -> Either Error Jwt
decode =
Expand Down