UNPKG

@fto-consult/common

Version:

Un ensemble de bibliothèques et d'utilistaires communs pour le développement d'applications javascript

42 lines (36 loc) 1.36 kB
// Copyright 2022 @fto-consult/Boris Fouomene. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. //import {JWT_SECRET} from "@env"; const JWT_SECRET = "@jwt-secrect" import JWT from 'expo-jwt'; import defaultStr from '../utils/defaultStr'; ///for more, @see : https://github.com/blake-simpson/expo-jwt export const algorithms = { HS256 : 'HS256', HS384 : 'HS384', HS512 : 'HS512', NONE : 'none', } export const defaultAlgorithm = "HS256"; export const getOptions = (options,key) =>{ if(key && typeof key =="object"){ options = typeof options =="object" && options ? options : key; } options = options && typeof options =='object' ? options : {}; options.algorithm = defaultStr(options.algorithm).toUpperCase(); if(!algorithms[options.algorithm]){ options.algorithm = defaultAlgorithm; } return options; } export const encode = (data,key,options) =>{ options = getOptions(options,key); key = key && typeof key =="string" ? key : JWT_SECRET; return JWT.encode(data,key,options); } export const decode = (token,key,options) =>{ options = getOptions(options,key); key = key && typeof key =="string" ? key : JWT_SECRET; return JWT.decode(token,key,options); }